Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Aaaagh!! passing addr of struct

Status
Not open for further replies.

jdeejay

Programmer
Oct 5, 2001
44
US
Can someone please help with this seemingly easy task. I have a struct and want to call another function that can manipulate the data within the struct.

I have a main where the struct is.
I have written a file to have the function.
And then I have written a mylib.h which is incl in each.

I can do this with an int to the function and manipulate it, but can't seem to get the syntax correct for a struct. Is there anything special about struct vs an int or char when doing this? What am I missing. I have ??? where I keep having the problem. I have written exactly as seen in two books, but still no.

**********************************
/* mylib.h ---my test header file */
void fnA (???)

*********************************
/* misfunc.c -- contains function declarations */

#include "mylib.h"

void fnA( ??? )
{
//do some stuff here;
}

********************************
/* test.c -- contains the main program */

#include "mylib.h"

typedef struct mystruct
{
//my structure schtuff
}Config;

main ()
{
Config myconfig = {/*initializers*/};
fnA ( &myconfig );

}


Thanks,
J

 
fnA should look like

void fnA( struct Config& cfg )
{
//do some stuff here;
}

Matt
 
However, you may be seeing problems because the function fnA does not know what Config is. Make sure the delclaration of the struct is before the function declaration.

Matt
 
Matt,

Thanks for the help. I put that in for both the misfunc.c and the mylib.h entry but I am still getting errors.

when compiling test.c - refers to the mylib.h file and says "parse error before '&'" also warnings that struct Config is declared inside parameter list and that its scope is only there which is probably not what I want.

Same messages appear when compiling the misfunc.c file.

In reference to your second post--the struct declaration is above the main just as appears. correct? I don't have to add the fnA prototype again to the test.c do I. I thought that was the purpose of the #include "mylib.h"

Any ideas?

John
 
Move the structure definition from test.c to mylib.h. Put it above the fnA prototype. Change "struct Config& cfg" to "struct Config* cfg" in the parameter lists. Still use "&" when calling the function though. I beleive the "*" is used to declare a pointer varible as well as dereferncing a pointer. The "&" is used to get the address of a non-pointer variable.
 
I'm not sure that you need "struct" in the parameter list either. It may not make a difference. Try "Config* cfg" in your parameter lists instead.

Good Luck!
 
Thanks,

That last response fixed the problem (after doing your first suggestion).

J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top