passing a struct

M

muser

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 )

}
 
J

jbruno4000

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.
 
V

Victor Bazarov

muser said:
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()
{
/******/

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
 

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

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top