473,626 Members | 3,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

some misunderstandin g 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(stru ct 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(*(str uct 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 5181
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(stru ct 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(*(str uct 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(stru ct 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(*(str uct 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(stru ct 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(*(str uct 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((stru ct 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(stru ct in_addr);
struct iphdr *ip;
struct in_addr temp_addr;
...
/* somewhere here 'ip' is filled in with values */
...
temp_addr.s_add r = 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(stru ct 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(*(str uct 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(addre ss);

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
1559
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 2.3.3 (#51, Dec 18 2003, 20:22:39) on win32 Type "help", "copyright", "credits" or "license" for more information.
3
1936
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 objects of my classes Person(superclass), Student(inherit Person), Teacher(inherit Person) in that array. The name will be the unique key. These classes are all working ok. I want to be able to add, remove, find etc. objects. To all of this, I...
7
2205
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 http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
16
2289
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 subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
48
2153
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
1413
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 the designer to use. The web project referecnes the data assembly with the TDS's in it. When I deploy this project, my Typed DataSets are still using the data in the app.config, and not the one in the web.config. Any idea on how to tell it to...
5
3360
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 I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran the authors example out of the book and quess what, it crashed also, : 0. I ran them both on my...
11
2961
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++, that anybody who doesn't use it might as well just program in plain C. I totally disagree with this, because I think C++ has a lot of great features apart from polymorphism, such as the ability to organize code into classes, code reuse through...
4
2771
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 done this before, I'm sure it's a rookie mistake but I cannot seem to find it. Can someone render some assistance please? struct Fpos { grib_handle *h;
0
8272
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5579
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.