How to code for pass one structure value to another structures?

Y

yezi

Hi:

How to code for operation between 2 structures?

The structure's defination is :
struct sockaddr_in my_addr;

I need call a subfunction and pass the value in my_addr to the
subfuncition , which same structure but different name?

Thanks for any comments.

bin
 
S

Singamsetty

yezi said:
Hi:

How to code for operation between 2 structures?

The structure's defination is :
struct sockaddr_in my_addr;

I need call a subfunction and pass the value in my_addr to the
subfuncition , which same structure but different name?

Thanks for any comments.

bin

........I'm not sure if I got your question correctly. I'm assuming you
want to know how to pass a structure (or it's members) to a function.
Here's an example:
struct foo {
int a;
int b;
};
struct foo foo1;

/*Passing entire structure*/
Function call: bar(&foo1);
Function defintion:
//if you want, you can use any other parameter name other than foo1
int bar(struct foo *foo1)
{
printf("%d, %d", foo1->a, foo1->b); //foo1 is a pointer so we use ->
return 0;
}

/*Passing a structure member*/
Function call: bar(&(foo1.a))
Function definition:
int bar(int *a)
{
printf("%d", a);
return 0;
}

HTH,
Hemanth
 
S

Singamsetty

Singamsetty said:
.......I'm not sure if I got your question correctly. I'm assuming you
want to know how to pass a structure (or it's members) to a function.
Here's an example:
struct foo {
int a;
int b;
};
struct foo foo1;

........I should have chosen a different variable name ( '1' looks like
a 'L') in my posting;-) No offense meant.

- Hemanth
 
V

venkatesh

you can pass struct to a function but the function proto type should be
like as foolows



return-type function-name(struct tag-name struct-name,other arg..(if
any));
if any mistake please report me
 
C

Christopher Benson-Manica

[original post was about passing a structure to a function]
Many thanks, that is what I want. the structure is too weird.

Furthermore, passing a structure by value (rather than a pointer to
the structure) usually is more costly in terms of runtime resources.
It's usually best to pass the pointer.
 

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
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top