Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer to array

I'm wondering, can you make a pointer to a group of variables in an array? like this

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

so then you could say...

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

and

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

and the length of *pointer

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

OK

Let me explain what I'm trying to accomplish, maybe that will clear me up a bit. I want to read a full file into a buffer, but I want to do it piece by piece. passing a small array into read() and then looping it into the larger array defeats the purpose. I was hoping that I could directly 'point' to an area in the buffer I want to fill, and pass that to the read() function.

I DO NOT want to use streams or anything that buffers behind my back

that would be counterproductive as I'm trying read the whole file into memory at once. As fast as possible.

I need speed!!!

like image 818
Kelly Elton Avatar asked Dec 15 '09 09:12

Kelly Elton


People also ask

What is pointer to an array?

CProgrammingServer Side Programming. Pointers are variables which stores the address of another variable. When we allocate memory to a variable, pointer points to the address of the variable. Unary operator ( * ) is used to declare a variable and it returns the address of the allocated memory.

Can we assign a pointer to array?

It is legal to use array names as constant pointers, and vice versa. Therefore, *(balance + 4) is a legitimate way of accessing the data at balance[4]. In the above example, p is a pointer to double, which means it can store the address of a variable of double type.

How do I make a pointer array in C++?

Consider this example: int *ptr; int arr[5]; // store the address of the first // element of arr in ptr ptr = arr; Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the address of the first element of the array in variable ptr .

Can you use a pointer as an array name?

We use the array name as a pointer to store elements into the array. After that, we print the elements of the array using the same pointer. The compiler creates a pointer by default while we create an array. We do not need to handle subscripts separately if we learn how to use these pointers efficiently.


1 Answers

Despite what everyone has said, this is possible, with one small caveat - unless you have a C99 compiler, you have to know the size of the "slice" that you want at compile time.

To declare and use the pointer correctly, you also have to know that the subscript operator [] binds before the * dereference operator, so parantheses are needed.

Behold:

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));

Addendum:

For the answer to your actual problem that you've put in, that's a lot easier. Just use a pointer that is set to an offset from the buffer array, eg:

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] */
like image 178
caf Avatar answered Nov 15 '22 07:11

caf