Help with passing void pointers by value in c functions via /r/learnprogramming


Help with passing void pointers by value in c functions

Hello all,

I am having trouble understanding an issue with my code and hope someone could point me in the right direction. So I have a collection that is meant to store elements of whatever type the user chooses to input. I then want to define a function that adds an element to that collection. Here is some of the code I have to create the collection in my implementation file:

static void *collection_ = NULL; static int index = 0; int Collection(size_t nElements, size_t elSize){ if((collection_ = malloc(nElements*elSize)) == NULL){ return 0; return 1; } } 

And here is the function I have to add an element to that collection:

int addElement(void *element){ collection_[index_*elSize_] = element; index_++; return 1; } 

Now the line

collection_[index_*elSize_] = element 

does not work, and it gets me an invalid use of void expression error. And I can't dereference void pointer, if I understood correctly. Could someone give me an idea of how to handle these types of situation? What would I have to do in order to get that element in that collection?

Thanks.

edits: mistakes/formatting.

Submitted July 11, 2017 at 10:10PM by Salavin
via reddit http://ift.tt/2tKZcUi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s