Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer of void array

I have an array of void-Pointers and want to access the elements (inititialize them), but it do not work:

void* anyptr = ...; //a pointer to something
void* arr = (void*)malloc(sizeof(void*)*10);

int i=0;
for(i=0; i<10; i++)
   *(arr+i) = anyptr; //dont work, (arr+n) = anyptr; doesn´t work too

I guess, the reason why this won´t work is that on the left side is the result of element i. But i don´t have an idea how to do this

like image 617
0xDEADBEEF Avatar asked Apr 20 '13 19:04

0xDEADBEEF


2 Answers

There are two ways to initialize arrays in C:

  • On the stack (which will handle memory for you since it will be cleaned up when your function ends)
  • In the heap (which will require you to handle allocation and freeing on your own).

If you would like to use the stack, you could initialize your array like this...

#define ARRAY_LENGTH 10
void *ptr;
void *arr[ARRAY_LENGTH];
for (int i = 0; i < ARRAY_LENGTH; i++) {
    arr[i] = ptr;
}

You can similarly define your array in the heap as follows...

#define ARRAY_LENGTH 10
void *ptr;
void **arr = malloc(sizeof(void *) * ARRAY_LENGTH);
for (int i = 0; i < ARRAY_LENGTH; i++) {
    arr[i] = ptr;
}
free(arr);

It is important to remember that an array (besides arrays assigned in the stack, which have some additional attributes such as length) is essentially just a pointer to the first element, and the operation arr[i] is the same as moving i*sizeof(elem) bytes away from the first element, and accessing the memory there. If you would like to get a pointer to the ith index in the array, then you would use notations such as...

void *indexPtr = arr + i;

or

void *indexPtr = &( arr[i] );

In this fashion, an array of void*'s would be of type void **, since the variable is a pointer to the first member of the array, which is a pointer. This can be a bit confusing, but just always try to keep in mind what type the elements of the array are, and creating a pointer to them. So if the array is of type int, then the array would be of type int or int[], but if you are storing pointers to integers, you would initialize an array of type int * in either of these two forms...

int **arr = malloc(sizeof(int *) * ARRAY_LENGTH);
int *arr[ARRAY_LENGTH];

Also note that you are storing pointers, so if you run the code...

int *arr[4];
for (int i = 0; i < ARRAY_LENGTH; i++) {
    arr[i] = &i;
}

Although it may seem to be that the values pointed to in the array would be as follows- [0, 1, 2, 3], but in reality it would be [4, 4, 4, 4], since what you actually have is an array of pointers all pointing to the variable i in your function, so whenever you change that, the values pointed to in the array will all be changed.
I hope this helped

like image 127
Joe Delgado Avatar answered Oct 05 '22 13:10

Joe Delgado


You need to change this line

void* arr = (void*)malloc(sizeof(void*)*10);

to this

void** arr = malloc(sizeof(void*)*10);
like image 31
Alexey Frunze Avatar answered Oct 05 '22 12:10

Alexey Frunze