473,545 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PLEASE HELP - Odd problem passing function arguments

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(Tem pAddrs);

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

printf("Receive d : %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.

Oct 5 '07 #1
8 1645
cp**********@ya hoo.com 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(Tem pAddrs);

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

printf("Receive d : %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
Oct 5 '07 #2
cp**********@ya hoo.com 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(Tem pAddrs);

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

printf("Receive d : %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.

--
Er*********@sun .com
Oct 5 '07 #3
"cp**********@y ahoo.com" <cp**********@y ahoo.comwrites:
In the calling function:

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

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

printf("Receive d : %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!
Oct 5 '07 #4
"cp**********@y ahoo.com" <cp**********@y ahoo.comwrites:
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(Tem pAddrs);

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

printf("Receive d : %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_Keit h) ks***@mib.org <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"
Oct 5 '07 #5
Eric Sosman <Er*********@su n.comwrites:
cp**********@ya hoo.com 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(Tem pAddrs);

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

printf("Receiv ed : %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.
Oct 5 '07 #6
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
Oct 5 '07 #7
Richard wrote On 10/05/07 18:08,:
>
>>cp**********@ yahoo.com wrote On 10/05/07 17:26,:
>>>
In the called function I have, right at the start:

printf("Rece ived : %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.

--
Er*********@sun .com
Oct 5 '07 #8
Eric Sosman <Er*********@su n.comwrites:
Richard wrote On 10/05/07 18:08,:
>>
>>>cp********** @yahoo.com wrote On 10/05/07 17:26,:

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

printf("Rec eived : %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 :-)
Oct 5 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
49561
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
3
14913
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
6
1738
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
7
4192
by: m7b52000 | last post by:
Some time ago I wrote a little program in Tcl/Tk that took the values from 3 sliders and performed a calculation using these values. The calculation was of course automatically repeated each time a slider was moved. It is proving most difficult in Python. How do I pass the .get() values to my calculating function? Do I use the command...
39
7605
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down indicate: a) That I don't know enough b) Passing arguments by ref is bad
36
1990
by: Christopher Benson-Manica | last post by:
This function is intended to print a string guaranteed to be in the form MM/DD/YYYY (assume <stdio.h> and <string.h> are included) as YYYY-MM-DD: void printdate( const char *date ) { char tdate, *cp; int mod=10, i; cp=tdate;
5
3902
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
31
2593
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
17
3576
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
10
12991
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var boldLink=document.getElementById('cmtbold');
0
7486
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7676
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6001
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5347
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4965
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
729
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.