PLEASE HELP - Odd problem passing function arguments

C

cpptutor2000

Could some C guru please help me?
I have a function that takes as an argument a pointer to an array of
unsigned chars (basically a hex representation of a dotted decimal IP
address). When I print out the received values in the receiving
function, I get something completely different from what I passed in.

The following are the relevant code snippets:
In the calling function:

unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
......
......
returnStatus = DoSomething(TempAddrs);

In the called function I have, right at the start:

printf("Received : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
addr[3]);

I am sure there is a problem, but I am not sure what it is - thanks in
advance for your help.
 
J

jacob navia

Could some C guru please help me?
I have a function that takes as an argument a pointer to an array of
unsigned chars (basically a hex representation of a dotted decimal IP
address). When I print out the received values in the receiving
function, I get something completely different from what I passed in.

The following are the relevant code snippets:
In the calling function:

unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
.....
.....
returnStatus = DoSomething(TempAddrs);

In the called function I have, right at the start:

printf("Received : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
addr[3]);

I am sure there is a problem, but I am not sure what it is - thanks in
advance for your help.
You are passing character strings and printing unsigned integers.

Either you change the printfs to %s or pass the numbers, and not
character strings!
 
E

Eric Sosman

Could some C guru please help me?
I have a function that takes as an argument a pointer to an array of
unsigned chars (basically a hex representation of a dotted decimal IP
address). When I print out the received values in the receiving
function, I get something completely different from what I passed in.

The following are the relevant code snippets:
In the calling function:

unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
.....
.....
returnStatus = DoSomething(TempAddrs);

In the called function I have, right at the start:

printf("Received : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
addr[3]);

I am sure there is a problem, but I am not sure what it is - thanks in
advance for your help.

The first line requires a diagnostic from the compiler.

So does the second.

And the third.

There's not enough information given to tell whether
there's anything wrong with the fourth line. It might
be a hopeless botch or it might be just fine; I can't
say.

The same holds for the final line: You've snipped
away so much that it's not possible to tell whether the
line is right or wrong.

Please post real code for a short, complete, compilable
program that demonstrates your problem. As things stand,
all I can say is that your difficulty is ..... or something
along those general lines.
 
B

Ben Pfaff

In the calling function:

unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
.....
.....
returnStatus = DoSomething(TempAddrs);

In the called function I have, right at the start:

printf("Received : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
addr[3]);

The elements of TempAddrs are strings, or, more precisely,
pointers to the first element of an array of char.

%u expects a unsigned int argument.

You can't mix those types like that.
 
K

Keith Thompson

Could some C guru please help me?
I have a function that takes as an argument a pointer to an array of
unsigned chars (basically a hex representation of a dotted decimal IP
address). When I print out the received values in the receiving
function, I get something completely different from what I passed in.

The following are the relevant code snippets:
In the calling function:

unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
.....
.....
returnStatus = DoSomething(TempAddrs);

In the called function I have, right at the start:

printf("Received : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
addr[3]);

I am sure there is a problem, but I am not sure what it is - thanks in
advance for your help.

You haven't shown us the declaration of DoSomething.

TempAddrs is an array of pointers to unsigned char. Evaluating the
name ``TempAddrs'' as a function argument gives you a pointer to
pointer to unsigned char. Is that what DoSomething expects to
receive? If not, turn up your compiler's warning level until it
complains.

You're representing each byte of the IP address as a string; for
example, you're representing the decimal value 192 as the character
sequence { '0', 'x', 'C', '0', '\0' }. You probably just want to
represent it as an unsigned char with the value 0xC0 (which you can
also write as 192); then a full IP address can be represented as an
array of four unsigned chars.

See also section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
 
R

Richard

Eric Sosman said:
Could some C guru please help me?
I have a function that takes as an argument a pointer to an array of
unsigned chars (basically a hex representation of a dotted decimal IP
address). When I print out the received values in the receiving
function, I get something completely different from what I passed in.

The following are the relevant code snippets:
In the calling function:

unsigned char* TempAddrs[] = {"0xC0", "0xA8", "0x00", "0x63"};
.....
.....
returnStatus = DoSomething(TempAddrs);

In the called function I have, right at the start:

printf("Received : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
addr[3]);

I am sure there is a problem, but I am not sure what it is - thanks in
advance for your help.

The first line requires a diagnostic from the compiler.

So does the second.

And the third.

There's not enough information given to tell whether
there's anything wrong with the fourth line. It might
be a hopeless botch or it might be just fine; I can't
say.

The same holds for the final line: You've snipped
away so much that it's not possible to tell whether the
line is right or wrong.

Please post real code for a short, complete, compilable
program that demonstrates your problem. As things stand,
all I can say is that your difficulty is ..... or something
along those general lines.

What does %u mean in a printf? It seems clear what the problem is
without a compilable sample - but maybe I am missing something?

He needs to use %s.
 
J

jacob navia

Richard said:
What does %u mean in a printf? It seems clear what the problem is
without a compilable sample - but maybe I am missing something?

He needs to use %s.

That's what I told him. Eric's answer is not really helpful.
 
E

Eric Sosman

Richard wrote On 10/05/07 18:08,:
(e-mail address removed) wrote On 10/05/07 17:26,:
In the called function I have, right at the start:

printf("Received : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
addr[3]);

[...]

What does %u mean in a printf?

"Print an unsigned int."
It seems clear what the problem is
without a compilable sample - but maybe I am missing something?

Perhaps you're missing the declaration of `addr'?
So are all the rest of us ...
He needs to use %s.

Maybe. I'm not sure.
 
R

Richard

Eric Sosman said:
Richard wrote On 10/05/07 18:08,:
(e-mail address removed) wrote On 10/05/07 17:26,:

In the called function I have, right at the start:

printf("Received : %u %u %u %u ...\n", addr[0], addr[1], addr[2],
addr[3]);

[...]

What does %u mean in a printf?

"Print an unsigned int."

It was a rhetorical question :)
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top