struct and call by reference

Joined
Apr 9, 2010
Messages
3
Reaction score
0
Hi!

How can I create a struct using a separate function? Here is my dream:

Code:
void createAny(Any* any)
{
  any = (Any*) malloc(sizeof(Any));
  printf("%0X ", any);
}

int main(){
	Any* any = NULL;

	printf("%0X ", any);
	
	createAny(any)

	printf("%0X ", any);

	getchar();
	return 0;
}

Thank you! :D
 
Joined
Apr 9, 2010
Messages
3
Reaction score
0
d'oh!

With malloc I use the pointer as a value. So it´s call by value, not call by reference - although using a pointer.

It is solved when passing the struct as a double pointer:

Code:
void createAny(Any** any)
{
  *any = (Any*) malloc(sizeof(Any));
  printf("%0X ", *any);
}

int main(){
	Any* any = NULL;

	printf("%0X ", any);
	
	createAny(&any)

	printf("%0X ", any);

	getchar();
	return 0;
}

Sometimes my brain cannot follow the path of C... :stupid:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top