By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

passing a struct

P: n/a
I have a struct declared in a function: void write_crecord( struct crecord *inc )
{
/******/

}

I now want to pass this into main

int main
{
/******/

switch(temp1[0])
case 'c':
case 'C':
Processcrecord( ...... )
write_crecord( how do I pass the struct to here, along with the pointer too )

}
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>I have a struct declared in a function: void write_crecord( struct crecord
*inc )
{
/******/

}

I now want to pass this into main

int main
{
/******/

switch(temp1[0])
case 'c':
case 'C':
Processcrecord( ...... )
write_crecord( how do I pass the struct to here, along with the pointer too )

}


Anything declared in a function is a local variable that ceases to exist once
the function terminates. Your struct should be declared in main and passed to
the function as a reference parameter.
Jul 22 '05 #2

P: n/a
"muser" <ch**********@hotmail.com> wrote...
I have a struct declared in a function: void write_crecord( struct crecord *inc )

What you have is an argument of a function declared to be of type
"a pointer to crecord", not "a struct declared in a function".
{
/******/

}

I now want to pass this into main
"Into main"?

int main
int main()
{
/******/

switch(temp1[0])
case 'c':
case 'C':
Processcrecord( ...... )
write_crecord( how do I pass the struct to here, along with the pointer too )

You cannot pass a type. You need an object. Do you have an object of
type 'crecord'?

}


the usual way is

void foo(struct abc* p) // struct abc _declared_
{
/**/
}

struct abc {}; // struct abc _defined_ here

int main()
{
abc def; // an object of type 'abc'
foo(&def); // passing the address of 'def'
}

Victor
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.