0

I'm trying to figure out how to work with void * arrays so that I can fill them up with any data type. For instance I'd like to fill up an array with integer values and then print the values of that array. To do this I've been trying to something like below but I'm pretty sure I have the totally wrong idea. How should I go about doing this correctly?

Fill array with ints

#define ARRAY_LENGTH 10
void *arr1;
for (int i = 0; i < ARRAY_LENGTH; i++) {
    void *indexPtr = arr1 + i*sizeof(int);
    indexPtr = &i;
}

Print array

for (int i=0; i < length; i++) {
    printf("new value %d\n", (int)&arr1[i]);
    (*insert)(list, &arr1[i]);
}
Community
  • 1
  • 1
Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109

3 Answers3

2

Fill array with ints:

#define ARRAY_LENGTH 10

/* expect arr1 to look at an already allocated memory chunk of type (void*) */

int *int_array = (int*)arr1;
for (int i = 0; i < ARRAY_LENGTH; i++) {
    int_array[i] = i;
}

Print array

for (int i=0; i < ARRAY_LENGTH; i++) {
 printf("new value %d\n", int_array[i]);
}

As pointer arithmetics on void* is illegal, you have to cast it to other pointer types. Casting it once in the beginning before using it is the easiest way; and in this case, to an integer pointer.

atturri
  • 1,133
  • 7
  • 13
1

If you don't malloc memory before trying to access your array, then your pointer will probably corrupt random addresses in memory.

So first thing to do:

#define ARRAY_LENGTH 10 // 10 Bytes

int main()
{
   char *char_arr;

   // Initial memory allocation
   char_arr = (char *) malloc(ARRAY_LENGTH);

   // [Do stuff]

   free(char_arr); // Don't forget to free!!!!

   return(0);
}

Then if you want to want to access your array using another type you must not break the strict aliasing rule. And as @atturri said pointer arithmetic is illegal with void pointers.

So you have to use a new pointer to access your array using an other type. And you have to take care of the upper bound of your new array:

int *int_array = (int*) char_arr;
int elementsCount = sizeof(*int_array) / sizeof(*char_arr);
for (int i = 0; i < elementsCount ; i++)
{
    int_array[i] = i;
}

Finally:

#define ARRAY_LENGTH 10 // 10 Bytes

int main()
{
   char *char_arr;
   int *int_array = (int*) char_arr;
   int elementsCount = sizeof(*int_array) / sizeof(*char_arr);
   int i;

   // Initial memory allocation
   char_arr = (char *) malloc(ARRAY_LENGTH);

    // Do stuff   
    for (i = 0; i < elementsCount ; i++)
    {
        int_array[i] = i*2;
        printf("int_array[%d]=%d", i, int_array[i]);
    }

   free(char_arr);
   /* Don't forget to free!! (Even if it's not "dangerous"
    * in this example in the main since we are about to quit */

   return(0);
}
Community
  • 1
  • 1
Plouff
  • 3,290
  • 2
  • 27
  • 45
-1

1st void* arr1 is not array - it just pointer to anything.

2nd - u can start work get address from some multibyte object convert it type as (void*) so u get void* arr1 that containt some value in it.

3nd for arr1[i] or (arr1+i) u must convert pointer arr1 to some type (int) (float*) etc (struct ustract*)

qulinxao
  • 183
  • 1
  • 10