473,499 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

some misunderstanding of pointers

Hello, All!

Short piece of code:

....
struct in_addr {
unsigned long int s_addr;
};

struct iphdr {
unsigned char ihl:4, version:4;
unsigned char tos;
short unsigned int tot_len;
short unsigned int id;
short unsigned int frag_off;
unsigned char ttl;
unsigned char protocol;
short unsigned int check;
unsigned int saddr;
unsigned int daddr;
};

extern char *inet_ntoa(struct in_addr);
struct iphdr *ip;
....
/* somewhere here 'ip' is filled in with values */
....
printf("%-15s ", inet_ntoa(ip->saddr));

Compiling with "gcc -Wall -g -ansi -pedantic" result with a warning: "
incompatible type for argument 1 of `inet_ntoa' ". I came to know the
solution is to change the call into:

printf("%-15s ", inet_ntoa(*(struct in_addr *)&ip->saddr));

But I can't entirely understand this construction - the conglomeration of
pointers and '&' operators, could you please explain it to me.

PS. It's not homework.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 15 '05 #1
5 5174
Roman Mashak wrote:

[ ...]
struct in_addr {
unsigned long int s_addr;
};

struct iphdr {
[snip members]
.... unsigned int saddr;
This is an unsigned integer.
unsigned int daddr;
};

extern char *inet_ntoa(struct in_addr);
And this expects a struct in_addr type *object* (term used with
caution),
to be passed to it.
struct iphdr *ip;
...
/* somewhere here 'ip' is filled in with values */
/* provided ip was allocated in the first place! */
...
printf("%-15s ", inet_ntoa(ip->saddr));
You are now passing an unsigned int instead of a struct in_addr.
Thus spake The Compiler:
incompatible type for argument 1 of `inet_ntoa' ". I came to know the
solution is to change the call into:

printf("%-15s ", inet_ntoa(*(struct in_addr *)&ip->saddr));
Coerce the compiler.
But I can't entirely understand this construction - the conglomeration of
pointers and '&' operators, could you please explain it to me.


ip->saddr is an unsigned int.
&ip->saddr is a pointer an unsigned int.
(struct in_addr *)&ip->saddr makes the compiler treat
the above as a pointer
to struct in_addr
*(struct in_addr *)&ip->saddr if possible, which it is, in *this*
case,
dereference the pointer to get an
object of
type struct in_addr
Luckily for you the struct in_addr
has only one member, and that is an unsinged int, so casting here
should not be a problem. However , think of a more general case,
and it would have been better if you had embedded a member in
the struct iphdr of type struct in_addr as follows:

struct iphdr {
....
/*unsigned int saddr;*/
struct in_addr sin_addr;
};
and done something like:

printf("%-15s ", inet_ntoa(ip->sin_addr));

This would of come handy were you change struct in_addr in future.
But that wouldn't have required changing struct iphdr.
HTH.

Nov 15 '05 #2

Roman Mashak wrote:
Hello, All!

Short piece of code:

...
struct in_addr {
unsigned long int s_addr;
};

struct iphdr {
unsigned char ihl:4, version:4;
unsigned char tos;
short unsigned int tot_len;
short unsigned int id;
short unsigned int frag_off;
unsigned char ttl;
unsigned char protocol;
short unsigned int check;
unsigned int saddr;
unsigned int daddr;
};

extern char *inet_ntoa(struct in_addr);
struct iphdr *ip;
...
/* somewhere here 'ip' is filled in with values */
...
printf("%-15s ", inet_ntoa(ip->saddr));

Compiling with "gcc -Wall -g -ansi -pedantic" result with a warning: "
incompatible type for argument 1 of `inet_ntoa' ". I came to know the
solution is to change the call into:

printf("%-15s ", inet_ntoa(*(struct in_addr *)&ip->saddr));

But I can't entirely understand this construction - the conglomeration of
pointers and '&' operators, could you please explain it to me.

PS. It's not homework.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru


The function is:

char *inet_ntoa(struct in_addr address);

Meaning: Function inet_ntoa, accepts an address parameter of type
"struct in_addr", and returns a pointer to data of type char.

Don't be fooled by the prototype. The "in_addr" is not a parameter name
but a type. Properly, it is "struct in_addr".

OK, so you are passing it:

inet_ntoa(*(struct in_addr *)&ip->saddr);

Meaning:

* = the data of
(struct in_addr *) = cast to a pointer to struct in_addr
& = the address of
ip->saddr = the integer saddr pointed to from ip

Now, re-arrange the above into English:

The data of, the address of the integer saddr in ip, cast to a pointer
of struct in_addr.

This is highly confusing. If I'm not mistaken, the code:

printf("%-15s ", inet_ntoa((struct in_addr)ip->saddr));

is what you really want but I'm guessing the compiler complained about
casting an int into a struct. Which is why you had to do the convoluted
thing you did.

An easier to understand code is:

extern char *inet_ntoa(struct in_addr);
struct iphdr *ip;
struct in_addr temp_addr;
...
/* somewhere here 'ip' is filled in with values */
...
temp_addr.s_addr = ip->saddr;
printf("%-15s ", inet_ntoa(temp_addr));
Just give the function what it wants and don't be too clever. The extra
temp variable is just 32 bits (typically), you're not saving that much
memory with your original method.

Nov 15 '05 #3
RM> Short piece of code:

RM> ...
RM> struct in_addr {

Thanks guys a lot for precious explanations!

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 15 '05 #4

Roman Mashak wrote:
RM> Short piece of code:

RM> ...
RM> struct in_addr {

Thanks guys a lot for precious explanations!

With best regards, Roman Mashak. E-mail: mr*@tusur.ru


No problem, glad to help..

Nov 15 '05 #5
Roman Mashak wrote:
...
struct in_addr {
unsigned long int s_addr;
};

struct iphdr {
unsigned char ihl:4, version:4;
unsigned char tos;
short unsigned int tot_len;
short unsigned int id;
short unsigned int frag_off;
unsigned char ttl;
unsigned char protocol;
short unsigned int check;
unsigned int saddr;
unsigned int daddr;
};

extern char *inet_ntoa(struct in_addr);
struct iphdr *ip;
...
/* somewhere here 'ip' is filled in with values */
...
printf("%-15s ", inet_ntoa(ip->saddr));

Compiling with "gcc -Wall -g -ansi -pedantic" result with a warning: "
incompatible type for argument 1 of `inet_ntoa' ". I came to know the
solution is to change the call into:

printf("%-15s ", inet_ntoa(*(struct in_addr *)&ip->saddr));


This makes the computer read from the memory allocated to ip->saddr, as
if it were actually of type struct in_addr.

There is a serious problem lurking here, which is when int and long have
a different size. On today's 32-bit computers, they are typically both
32 bits. However, on 64-bit computers, it is common for long to be 64
bits while int is still 32 bits. This will cause the computer to read an
extra 4 bytes after saddr, effectively the value of daddr, and combine
the two values together to produce a single huge value.

Here's a better solution, using a temporary variable:

struct in_addr address;
address.s_addr = ip->saddr;
printf("%-15s ", inet_ntoa(address);

Here the conversion from unsigned int to unsigned long is done
correctly, preserving the value instead of possibly reading extra memory
that it shouldn't read.

--
Simon.
Nov 15 '05 #6

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

Similar topics

14
1535
by: Alexander May | last post by:
When I define a function in the body of a loop, why doesn't the function "close" on the loop vairable? See example below. Thanks, Alex C:\Documents and Settings\Alexander May>python Python...
3
1925
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
7
2186
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
16
2262
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...
48
2114
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
6
1397
by: Shawn Wildermuth | last post by:
I have a web project whose web.config has a ConnectionString setup. I have a second assembly with my Typed DataSets. The designer has created an App.config file with a connection string in it for...
5
3337
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
11
2943
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
4
2754
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
7134
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,...
0
7014
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...
0
7180
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,...
0
7229
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...
1
6905
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...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
311
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...

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.