C: printf str

Feb 7, 2009 at 12:46pm
hi all,
1
2
3
4
5
6
7
8
for (i=0;i<4; i++) {
      id[i] = (address >> (24-i*8)) & 0xFF;
	p->srt += ntohs(id[i]);
	if(i < 3) {
p->srt += ntohs(".");
}
printf("%u",p->srt);
}

I just need with it...
collect id[i] with i from 0 to 3 and separated "."(point) and print it stuff
str = id[0].id[1].id[2].id[3];
printf("%u",p->srt);
how to doing it format?
Feb 7, 2009 at 3:05pm
What is p?

You have the right idea, you just have a bug or two. Line 5 you should not ntohs("."). If p->srt is a string, then p->srt += '.' would work.

Then you need to print it as a string (%s), not as an unsigned (%u).
Feb 8, 2009 at 4:04am
What is p?

It's a pointer on struct.
Then you need to print it as a string (%s), not as an unsigned (%u).

No, because I've at least 16 chars, therefore I need use long type such as unsigned...
unsigned char id[4];
unsigned char str;
What's function can take unsigned char and return unsigned char?
Feb 8, 2009 at 4:45am
No, because I've at least 16 chars, therefore I need use long type such as unsigned...
I'm intrigued. Could you please explain where or how you got that idea?
Feb 8, 2009 at 6:14am
Hm, possible I do a mistake. Because function ntohs would be return int, I need something what can be returned unsigned char or just char.
strcat( p->str, itoa( id[i] ) );
Or what's function I can use without strcat, because if I tried use itoa I've got that"undefined reference to `itoa'", but it defined on <stdlib.h>

Or which is another way to work with C-strings?
Last edited on Feb 8, 2009 at 6:20am
Topic archived. No new replies allowed.