representation of integers(again) very annoying..

M

Mantorok Redgormor

I have a member of a struct which is:
int32_t ut_addr_v6[4];
And int32_t is typedef int int32_t; mentioning that for clarity.

Now when I attempt the following:
printf("%u.%u.%u.%u\n", ut_addr_v6[0],
ut_addr_v6[1], ut_addr_v6[2],
ut_addr_v6[3]);

I get: 2912244697.0.0.0

Why is it that I get this result from the above?

However, if I use the following:

void *ptr;
unsigned char *ptr1;

ptr = ut_addr_info;
ptr1 = ptr;

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

It displays how I wish it to display. So why is it that it works for a
pointer to unsigned char but doesn't work when I attempt to display
each element of the array of ints? And why is it that the array of its
is 4 ints and not just one, when all it seems to occupy is one int? Is
this for compensation of support for ipv6? If that is the case, then I
must be accessing the individual bytes of the first element of the
array with unsigned char *.

Also, what is a good method/algorithm for converting network byte
order to host byte order where host byte order is little endian? I can
extract the individual bytes in reverse order and stoe them in an
array but I was aiming more for storing them in an object of the same
type I am manipulating.

And lastly, when I was using the "ptr" of type void * above, I did the
following:

printf("%u.%u.%u.%u\n", (unsigned char)ptr[0],
(unsigned char)ptr[1], (unsigned char)ptr[2],
(unsigned char)ptr[3]);

dereferencing of void *.

Is this because the cast has no affect when ptr[0] is firstly
converted to *(ptr+0) ? What is a way around this while still using a
pointer to void?


- nethlek
 
B

Barry Schwarz

I have a member of a struct which is:
int32_t ut_addr_v6[4];
And int32_t is typedef int int32_t; mentioning that for clarity.

Now when I attempt the following:
printf("%u.%u.%u.%u\n", ut_addr_v6[0],
ut_addr_v6[1], ut_addr_v6[2],
ut_addr_v6[3]);

Is it really part of a struct? You have no struct name in the code.

Based on what you provided it seems as if an int is 32 bits. Is this
correct?

%u is the format for an unsigned int. You are passing a signed int.
Normally a mismatch between format and argument causes undefined
behavior.
I get: 2912244697.0.0.0

Why is it that I get this result from the above?

However, if I use the following:

void *ptr;
unsigned char *ptr1;

ptr = ut_addr_info;
ptr1 = ptr;

You could have simply said
ptr1 = (void*)ut_addr_info;

Which is it, ut_addr_info or ut_addr_v6?
printf("%u.%u.%u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);

Here the %u is correct because an unsigned char will be automatically
promoted to unsigned int when used as an argument in a variadic
function.

But each ptr is only one char. (Pls confirm that on your system a
char is 8 bits.) That means that ptr1[0] through ptr1[3] are the four
bytes of ut_addr_v6[0].
It displays how I wish it to display. So why is it that it works for a
pointer to unsigned char but doesn't work when I attempt to display
each element of the array of ints? And why is it that the array of its

You are not printing the same data. In one case, you are printing the
4 bytes of one int, each as a separate value. In the other, you are
printing this one int in its entirety and three other int as well.
is 4 ints and not just one, when all it seems to occupy is one int? Is
this for compensation of support for ipv6? If that is the case, then I

It is your code; how would we know.
must be accessing the individual bytes of the first element of the
array with unsigned char *.

That is common for dealing with ip addresses.
Also, what is a good method/algorithm for converting network byte
order to host byte order where host byte order is little endian? I can
extract the individual bytes in reverse order and stoe them in an
array but I was aiming more for storing them in an object of the same
type I am manipulating.

If you are talking about unsigned int, you could use
unsigned_int = ((unsigned)first_byte << 24) |
((unsigned)second_byte << 16) |
((unsigned)third_byte << 8) |
fourth_byte;
And lastly, when I was using the "ptr" of type void * above, I did the
following:

printf("%u.%u.%u.%u\n", (unsigned char)ptr[0],
(unsigned char)ptr[1], (unsigned char)ptr[2],
(unsigned char)ptr[3]);

dereferencing of void *.

Is this because the cast has no affect when ptr[0] is firstly
converted to *(ptr+0) ? What is a way around this while still using a
pointer to void?

Cast and [] have the same precedence and associate left to right so it
should have been processed as ((unsigned char)ptr)[0] which never
dereferences a void *. Post some compilable code so we can see for
ourselves.


<<Remove the del for email>>
 
P

pete

Barry said:
On 12 Oct 2003 17:04:51 -0700, (e-mail address removed) (Mantorok Redgormor)
wrote:
printf("%u.%u.%u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);

Here the %u is correct because an unsigned char will be automatically
promoted to unsigned int when used as an argument in a variadic
function.

unsigned char will be converted to int,
if int can represent all values of the original type.
 
B

Barry Schwarz

I have a member of a struct which is:
int32_t ut_addr_v6[4];
And int32_t is typedef int int32_t; mentioning that for clarity.

Now when I attempt the following:
printf("%u.%u.%u.%u\n", ut_addr_v6[0],
ut_addr_v6[1], ut_addr_v6[2],
ut_addr_v6[3]);

Is it really part of a struct? You have no struct name in the code.

Based on what you provided it seems as if an int is 32 bits. Is this
correct?

%u is the format for an unsigned int. You are passing a signed int.
Normally a mismatch between format and argument causes undefined
behavior.
I get: 2912244697.0.0.0

Why is it that I get this result from the above?

However, if I use the following:

void *ptr;
unsigned char *ptr1;

ptr = ut_addr_info;
ptr1 = ptr;

You could have simply said
ptr1 = (void*)ut_addr_info;

Which is it, ut_addr_info or ut_addr_v6?
printf("%u.%u.%u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);

Here the %u is correct because an unsigned char will be automatically
promoted to unsigned int when used as an argument in a variadic
function.

But each ptr is only one char. (Pls confirm that on your system a
char is 8 bits.) That means that ptr1[0] through ptr1[3] are the four
bytes of ut_addr_v6[0].
It displays how I wish it to display. So why is it that it works for a
pointer to unsigned char but doesn't work when I attempt to display
each element of the array of ints? And why is it that the array of its

You are not printing the same data. In one case, you are printing the
4 bytes of one int, each as a separate value. In the other, you are
printing this one int in its entirety and three other int as well.
is 4 ints and not just one, when all it seems to occupy is one int? Is
this for compensation of support for ipv6? If that is the case, then I

It is your code; how would we know.
must be accessing the individual bytes of the first element of the
array with unsigned char *.

That is common for dealing with ip addresses.
Also, what is a good method/algorithm for converting network byte
order to host byte order where host byte order is little endian? I can
extract the individual bytes in reverse order and stoe them in an
array but I was aiming more for storing them in an object of the same
type I am manipulating.

If you are talking about unsigned int, you could use
unsigned_int = ((unsigned)first_byte << 24) |
((unsigned)second_byte << 16) |
((unsigned)third_byte << 8) |
fourth_byte;
And lastly, when I was using the "ptr" of type void * above, I did the
following:

printf("%u.%u.%u.%u\n", (unsigned char)ptr[0],
(unsigned char)ptr[1], (unsigned char)ptr[2],
(unsigned char)ptr[3]);

dereferencing of void *.

Is this because the cast has no affect when ptr[0] is firstly
converted to *(ptr+0) ? What is a way around this while still using a
pointer to void?

Cast and [] have the same precedence and associate left to right so it
should have been processed as ((unsigned char)ptr)[0] which never
dereferences a void *. Post some compilable code so we can see for
ourselves.


Disregard the last paragraph. Reading too quickly. It is a matter of
precedence. [] has higher precedence than cast so this is interpreted
as (unsigned char)(ptr[0]) which does in fact dereference the void
pointer. You could have used ((unsigned char*)ptr)[0].


<<Remove the del for email>>
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top