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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to pass a structure pointer to a function?

Status
Not open for further replies.

greadey

Technical User
Oct 25, 2001
231
GB
Here's a snippet of code;

// a simple structure for my experiment
struct new {

int i;
int j;
int k;

};
// This won't compile!
float some_func( new *);

// I can do this though

struct new mystruct;
struct new *pMystruct;

so if I can declare a pointer to the strcuture, why can't I seem to declare a function that takes a pointer to the structure as an argument.

Thanks.

greadey
 
Code:
// This won't compile!
float some_func( new *);

post the compiler error.

did you try changing 'new' to something else like 'foo'? could be keyword problem.

-pete
 
Got it sussed now thanks,

float some_func(struct foo *);

works :))
 
Get a grip on struct defs and type declarations with typedef.
When defining a structure with struct xxx {...}, the structure will be fully refered by its name, along with the type specifier struct.

As a good practice, you should have the typedefs right after declaring the structure and declare all the functions with the typedefs:

float some_func(pMystruct); it will always work. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Actually we can pass the a structure-typed argument into the function. But, passing the pointer argument into the function will essentially increase the performance of your program.

Let's say, you've a function that call a same function for many times in the loop, and you pass a structure-typed variable that has byte sizes of 20 bytes or more. It'll increase the time when call and return to/from function.

But if your use a pointer variable (pointer to structure variable) as argument, the variable sizes are only 4 (normally)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top