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 4 2063
On 12 Oct 2003 17:04:51 -0700, ne*****@tokyo.com (Mantorok Redgormor)
wrote: 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[i] 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>>
In article <41**************************@posting.google.com >, ne*****@tokyo.com (Mantorok Redgormor) wrote: <snipped>
I think you should take a course about programming in C.
Barry Schwarz wrote: On 12 Oct 2003 17:04:51 -0700, ne*****@tokyo.com (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.
--
pete
On 13 Oct 2003 05:36:46 GMT, Barry Schwarz <sc******@deloz.net> wrote: On 12 Oct 2003 17:04:51 -0700, ne*****@tokyo.com (Mantorok Redgormor) wrote:
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[i] 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>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chris Dutrow |
last post by:
I searched around on the net for a bit, couldn't find anything though.
I would like to find some code for a function where I input A Range Of
Integers
For example: Function( 1, 100 );
And the...
|
by: Arun Goel |
last post by:
Hi,
I came across this question from a website..
According to the C++ standard, what is an object's internal representation
in memory guaranteed to be?
a) Initialized
b) On a word boundary...
|
by: Mantorok Redgormor |
last post by:
I always see posts that involve the representation of integers, where
some poster claims that the unerlyding representation of an integer
doesn't have to reflect on the actual integer, for example:...
|
by: junky_fellow |
last post by:
According to Section A6.6 Pointers and Integers (k & R)
" A pointer to one type may be converted to a pointer to another type.
The resulting pointer may cause
addressing exceptions if the...
|
by: Ruffin Bailey |
last post by:
I coulda sworn I was given an explanation during an AppDev class years
ago for VB6, but don't recall the answer. Why is it that -1 is True
in Visual Basic (and now VB.NET)? Bit flags seem like...
|
by: yossi.kreinin |
last post by:
Hi!
There is a system where 0x0 is a valid address, but 0xffffffff isn't.
How can null pointers be treated by a compiler (besides the typical
"solution" of still using 0x0 for "null")?
-...
|
by: Steve Carter |
last post by:
The following code fragment prompts the user to enter two integers
separated by a space. It works the way I want it to, but a friend
said it's not correct and said it can cause a buffer overflow. I...
|
by: Gary Wessle |
last post by:
Hi there
I have a method which returns time_t and another two methods return
double data types and I cann't change that since the library is
provided by Big Bucks Inc. I think time_t is long but...
|
by: Carramba |
last post by:
Hi!
How can I output value of char or int in binary form with printf(); ?
thanx in advance
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
| |