Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying struct in function signature

Say I have

struct mystruct
{
};

Is there a difference between:

void foo(struct mystruct x){}

and

void foo(mystruct x){}

?

like image 488
Luchian Grigore Avatar asked Sep 08 '11 13:09

Luchian Grigore


2 Answers

In C the latter isn't valid.

However in C++ they're almost the same: The first one would be valid if you haven't yet declared your struct at all, it would treat it as a forward declaration of the parameter all in one.

like image 74
Mark B Avatar answered Oct 29 '22 09:10

Mark B


Not in the code you've written. The only difference I know of between using a defined class name with and without struct is the following:

struct mystruct
{
};

void mystruct() {}

void foo(struct mystruct x){} // compiles
void foo(mystruct x){} // doesn't - for compatibility with C "mystruct" means the function

So, don't define a function with the same name as a class.

like image 7
Steve Jessop Avatar answered Oct 29 '22 10:10

Steve Jessop