Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Void type parameter in C

Hello there I am working on an assignment in C where I need to pass in an unknown type of parameter into a function.

For example suppose I have the following:

int changeCount(void* element)
{
    element.Count = element.Count++;

    return 1;

}

The reason why variable element is void is because there are 3 types of possibilities. All 3 however do have a member variable named "Count".

When I try to compile the actual code I wrote in Eclipese, I get the following error:

error: request for member ‘Count’ in something not a structure or union

I am guessing this is happening because the compiler doesn't know the type of "element" before hand. However I don't see why this isn't working.

Thanks for help!

like image 282
Adam Lee Avatar asked Nov 21 '09 17:11

Adam Lee


People also ask

Can we pass void as parameter in C?

But in C, main() will be called with any number of parameters. main(void) will be called without any parameters. If we try to pass it then this ends up leading to a compiler error.

What is void type in parameter C?

void (C++) When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal."

Is there a void type in C?

The void type, in several programming languages derived from C and Algol68, is the return type of a function that returns normally, but does not provide a result value to its caller. Usually such functions are called for their side effects, such as performing some task or writing to their output parameters.

How many ways pass parameters in C?

There are two ways to pass parameters in C: Pass by Value, Pass by Reference.


3 Answers

You need to cast the pointer to one of those 3 types and then use it. Something like:

MyType* p = (MyType*)element;
p->count++;

However, you need to be sure of the type of the object you are casting as casting an object to the wrong type can be dangerous.

like image 97
Naveen Avatar answered Oct 24 '22 04:10

Naveen


First, you are passing in a pointer to void, which is a valid approach for unknown types, but you need to be passing in pointers to your different object types.

C isn't a dynamic language so symbolic type information is largely deleted before run time so when you say that your three types all have a member Count this doesn't help with your function design.

The only way you can access Count is by casting your void* parameter to the correct pointer type before derefencing with -> or (*element).Count (i.e. not just .).

Unless you are relying on your types having a compatible layout (which is likely to be implementation dependent) you will also need to pass something that helps your function determine the correct cast to perform. At this point you may be better off with three seperate functions and better type safety.

like image 42
CB Bailey Avatar answered Oct 24 '22 03:10

CB Bailey


You have to cast it to the actual type but you may get unexpected results in the structs don't have the count property in the same place.

typedef struct _A 
{
    int count;
    int x;
}A;
int changeCount(void* element)
{
    ((A*)element)->Count++;

    return 1;

}

Also remember that if you pass a pointer to a struct that looks like the following you will increment the x field not the count.

typedef struct _B 
{
    int x;      
    int count;

}B;

    B st;
    B* pst = &st;
    changeCount(pst);
like image 44
rerun Avatar answered Oct 24 '22 04:10

rerun