Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > PLEASE HELP - Odd problem passing function arguments

Reply
Thread Tools

PLEASE HELP - Odd problem passing function arguments

 
 
cpptutor2000@yahoo.com
Guest
Posts: n/a
 
      10-05-2007
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.

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      10-05-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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!


--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-05-2007
(E-Mail Removed) wrote On 10/05/07 17:26,:
> 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.

--
(E-Mail Removed)
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      10-05-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> 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.
--
Here's a tip: null pointers don't have to be *dull* pointers!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-05-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> 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/>.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      10-05-2007
Eric Sosman <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote On 10/05/07 17:26,:
>> 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.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      10-05-2007
Richard wrote:
>
> 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.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-05-2007
Richard wrote On 10/05/07 18:08,:
>
>>(E-Mail 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.

--
(E-Mail Removed)
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      10-05-2007
Eric Sosman <(E-Mail Removed)> writes:

> Richard wrote On 10/05/07 18:08,:
>>
>>>(E-Mail 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
function call with arguments which takes no arguments Neo C Programming 10 01-20-2005 06:31 AM



Advertisments