passing a value of a variable from one function to another

K

Kifah Abbad

hi guys,

Supposidly i have a function

code:--------------------------------------------------------------------------------
decode_ip(u_char *packet, u_char flags)
--------------------------------------------------------------------------------


that has a variable which is


code:--------------------------------------------------------------------------------
char destination_ip[40]; //Here we save destination IP
--------------------------------------------------------------------------------


where we save a specific value

code:--------------------------------------------------------------------------------

/*Kifah this part is for saving destination IP somewhere*/
sprintf(destination_ip,"%d.%d.%d.%d",
(packet[16] & 0xff),
(packet[17] & 0xff),
(packet[18] & 0xff),
(packet[19] & 0xff));
--------------------------------------------------------------------------------



Now i wanna "pass" the value saved in it, to another function:


code:--------------------------------------------------------------------------------

void socket_connect(struct scoop_pack *vp)
{
..
..
..
//something maybe like?
their_addr.sin_addr.s_addr = inet_addr(destination_ip);
--------------------------------------------------------------------------------



Is that possible? how would be able to pass it then?

Thanks in advance
 
K

Kurt Watzka

Kifah said:
hi guys,

Supposidly i have a function
that has a variable which is
char destination_ip[40]; //Here we save destination IP

Now i wanna "pass" the value saved in it, to another function:

their_addr.sin_addr.s_addr = inet_addr(destination_ip);
Is that possible? how would be able to pass it then?

You pass destination_ip (which decays to a pointer to char, which
is compatible with a pointer to a const qualified char in case
your inet_addr() happens to expect a pointer to a const qualified
char) to inet_addr().

You cannot pass an array to a function, but in most cases, your's
included, you don't want to. You cannot pass "variables" to functions
in C, but you can pass values of expressions.

The parameters are local variables of your function that are initialized
with the values that you pass to the function. The value of the expression

destination_ip

is a pointer to the first element of destination_ip. If inet_addr() happens
to be defined as

funny_type inet_addr(char const *verbose_address)
{
}

then verbose_address inside inet_addr() starts its life initialized with
the value of the expression you passed to inet_addr(), i.e. a pointer to
the first element if destination_ip.

Kurt
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top