CrispyCashew CrispyCashew - 1 month ago 14
C Question

Typecast void pointer to int and string

What im trying to do is store values in a array of Element strucs (that has a member void* data)

typedef struct {
void* data
} Element;


I have malloc'd the array to have 3 elements

Element* array = malloc(3 * sizeof(Element));


I know you can do it like this if data is a int but how do you do it if data is void *

array[0].data = 65;


ive tried typecasting

((*int)(array[0].data)) = 65;


but I get the message expected expression before 'int'

My second question is how would I go about storing a string in the Element struc (I cant make another member that is a string it has to be using the void pointer)

Can I use strcopy or sscanf ?

Sample main below incase im doing something wrong

#include "test.h"
int main (void)
{
Element* array = malloc(3 * sizeof(Element));
((*int)(array[0].data)) = 65;
((*int)(array[1].data)) = 64;
((*int)(array[2].data)) = 66;
}


EDIT

Sorry I should have added that the data will be added using a loop (the iterations is based on user input, so arrayOfElements could have 5,8,20 ect elements), based on user3386109 answer

Element* arrayOfElements = malloc(3 * sizeof(Element));
arrayOfElements[0].data = malloc( sizeof(int) );
int *ptr = arrayOfElements[0].data;
*ptr = 65;


work perfectly but if I want to make arrayOfElements[1].data = 67

Element* arrayOfElements = malloc(3 * sizeof(Element));
arrayOfElements[1].data = malloc( sizeof(int) );
int *ptr = arrayOfElements[1].data;
*ptr = 76;


I cant reuse *ptr even if I do free(ptr) , is there anyway to do this if you dont know how many elements arrayOfElements will have?

Answer

The malloc in your question creates an array of three structs. Each struct has an uninitialized pointer called data. Before you use that pointer you must initialize it with another call to malloc.

To store an int:

array[0].data = malloc( sizeof(int) );
int *ptr = array[0].data;
*ptr = 65;

To store a string:

array[0].data = malloc( strlen(str) + 1 );
strcpy( array[0].data, str );

Here's a complete example that shows how to initialize the array, print the elements of the array, and then free the array

int main( void )
{
    int *ptr;
    char *str = "hello";

    Element *array = malloc(3 * sizeof(Element) );

    array[0].data = malloc( sizeof(int) );
    ptr = array[0].data;
    *ptr = 65;

    array[1].data = malloc( strlen(str) +  1 );
    strcpy( array[1].data, str );

    array[2].data = malloc( sizeof(int) );
    ptr = array[2].data;
    *ptr = 76;

    ptr = array[0].data;
    printf( "%d\n", *ptr );
    str = array[1].data;
    printf( "%s\n", str );
    ptr = array[2].data;
    printf( "%d\n", *ptr );

    for ( int i = 0; i < 3; i++ )
        free( array[i].data );
    free( array );
}
Comments