Array pointer

I am wondering if you can make a pointer to a group of variables in an array? like this

array[20]{'a','b','c',...}
pointer = array[6 through 10];

so you can say ...

*pointer[0] == array[6];

and

*pointer[5] == array[10];

and pointer length *

5 == sizeof(*pointer) \ sizeof(type);

Ok

Let me explain what I'm trying to accomplish, maybe this will remove me a little. I want to read the full file into the buffer, but I want to do this in parts. passing a small array to read (), and then looping it over to a larger array, defeats the target. I was hoping that I could directly “point” to the area in the buffer that I want to fill, and pass this to the read () function.

I do NOT want to use streams or anything that buffers behind me

which would be counterproductive as I try to read the entire file in memory immediately. As soon as possible.

I need speed !!!

+5
10

, , - C99, "", .

, , [] *, .

:

int array[20] = {'a','b','c','d','e','f','g','h','i','j','k','l'};
int (*pointer)[10 - 6 + 1] = (int (*)[10 - 6 + 1])&array[6];    /* = array[6 through 10] */

printf("(*pointer)[0] = %c\n", (*pointer)[0]);
printf("(*pointer)[4] = %c\n", (*pointer)[4]);
printf("sizeof *pointer / sizeof **pointer = %lu\n", (unsigned long)(sizeof *pointer / sizeof **pointer));

:

, , . , , :

unsigned char buffer[102400];
unsigned char *ptr;

/* ... */
ptr = buffer + 500;
read(fd, ptr, 1024); /* Try and read up to 1024 bytes at position buffer[500] */
+12

, .

int a[4] = {1,2,3,4};
int *b = &a[2];
cout << b[0] << "-" << b[1] << endl;

3-4.

+6

10x ints. 2 - , . , , : for, p1, , p2 - . ​​

, ?

#include <iostream>
using namespace std;

int main()
{ 
    int a[10] = { 0, 4, 5, 7, 4, 3, 1, 6, 2, 9 };

    int *p1 = &a[3];
    int *p2 = &a[7];

    for(int *p = p1; p != p2; ++p)
    {
        cout << *p << endl;
    }

    return 0;
}
+4

, . pointer = array + 6. pointer? :

char pointer[5];

. :

char *pointer = array + 6;

, ( l). :

char a[10];
char b[10];
b = a;

, C, - &mdash, " " , . , , , sizeof.

, sizeof array 10 ( 10 ), sizeof pointer , char. , , , C. ++ , , .

: :

, , , :

unsigned char bigbuf[1UL << 24]; /* or whatever you want */
unsigned char *ptr = bigbuf;
size_t chunk = BUFSIZ;
FILE *fp = fopen("foo.txt", "rb"); /* no error checking */
size_t nread;
size_t total = 0;
while ((nread = fread(ptr, 1, chunk, fp)) > 0) {
    ptr += nread;
    total += nread;
    if (total + chunk > sizeof bigbuf) {
        /* oops, not enough space */
    }
}
if (ferror(fp)) {
    /* read error */
} else {
    /* bigbuf now has data, total has the number of bytes */
}

- setvbuf(). , , . .

, , , , :

, , .. :

T a[10];

T, pointer , :

pointer[1] == a[0]
pointer[2] == a[1]
...
pointer[11] == a[10]

( , 1 LHS ).

, C, no. , Numericical Recipes in C "".

, ,

pointer[0] == array[6];

*pointer[0] == array[6];

( *pointer[5])

+4

, C. C "" , "" C . , , .

+3

, , , ,

char *p = array + 6;

, .

, , ,

array[i] 

*(array + i)
+3

pointer = &array[6]. pointer[0] , array[6]. sizeof. - :

template <class T>
class Slice {
public:
 Slice(T* elements, int begin, int end) : m_elements(elements + begin), m_size(end - begin) {}
 T& operator[] (int index) {return m_elements[index];}
 int size() const {return m_size;}
private:
 T* m_elements;
 int m_size; 
};

int values[10];
Slice<int> slice(values, 5, 10);
values[5] = 3;
assert(slice[0] == values[5]);
assert(slice.size() == 5);
+2

, :

int* ptr = new int[10];
int* ptrSub = &ptr[5];
+2

, , , .

++ - , arrayclass ( + ). -, : , .

Plain-Old-C , : . C- " " , () , .

, :

int  array1[10];
int  array2[3];

// reference 3 elements from array1, starting at element 5 in array1
array2 = array1[5];

. "array2" , - : , .

+1

Pointers only "point" to the first element in any structure or variable. This is just an address. You can specify a range by making them a pointer to a specific type that identifies the range.

-1
source

All Articles