473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Doubt: Regarding pointer addition

Hi,
I have a doubt in the following code:
struct xyz
{
int x;
long l;
float f;
};
int main()
{
struct xyz xy;
unsigned int i = (&xy + 1)- &xy;
printf( "%u \n",i);
return 0;
}
In the above example &xy cancels each other and the code returns a 1.

My question is :
I feel this should give the difference in memory addresses as the
answer (say size of struct (with alignments ))
why doesnt the compiler takes into account the type of xy as pointer
and do a pointer addition. Is this behaviour in conformance with the C
standard or am I missing something??????
Regards
Reji

Dec 13 '06 #1
6 2118
re*********@symantec.com wrote:
Hi,
I have a doubt in the following code:

include <stdio.h>
struct xyz
{
int x;
long l;
float f;
};
int main()
int main (void) is preferred
{
struct xyz xy;
unsigned int i = (&xy + 1)- &xy;
printf( "%u \n",i);
return 0;
}
In the above example &xy cancels each other and the code returns a 1.
Which seems correct.
>
My question is :
I feel this should give the difference in memory addresses as the
answer (say size of struct (with alignments ))
why doesnt the compiler takes into account the type of xy as pointer
and do a pointer addition. Is this behaviour in conformance with the C
standard or am I missing something??????
Your compiler does take care of pointer math.
Isn't (&xy + 1) at a distance of 1 times (sizeof struct xyz) from &xy ?

Casting pointes to char * might give you the results you expect.

unsigned int i = (char *) (&xy + 1) - (char *)&xy;

- p_cricket_guy

Dec 13 '06 #2
In article <11**********************@j72g2000cwa.googlegroups .com>,
<re*********@symantec.comwrote:
>I have a doubt in the following code:
"dobut" is not used that way in English, but that's a topic that
has been covered in a number of previous threads.

>struct xyz
{
int x;
long l;
float f;
};
int main()
{
struct xyz xy;
unsigned int i = (&xy + 1)- &xy;
printf( "%u \n",i);
return 0;
}
>In the above example &xy cancels each other and the code returns a 1.
>My question is :
I feel this should give the difference in memory addresses as the
answer (say size of struct (with alignments ))
why doesnt the compiler takes into account the type of xy as pointer
and do a pointer addition. Is this behaviour in conformance with the C
standard or am I missing something??????
It is the way that pointer arithmetic is defined.

There is no way in C to get the "memory address" of an object: you can
only get a representation of a pointer to the object. Pointers do not
have to be anything like memory addresses. For example, on DEC's VMS, a
pointer might be the address of a "descriptor" that tells the hardware
information about the type of the object and about the size of the
object, so type-checking and array-bounds checking could be done in
hardware.

Pointer arithmetic is defined by the C standard so that p+1 is
the next object (of the same type) after p. Think of it like
arrays: p[0] is the first object, p[1] is the second object,
p[2] is the third, and so on: you increment the subscript to designate
the next member. In C, pointer arithmetic is the difference
in subscripts, not the difference in memory addresses.

In C, the "difference in subscript of arrays" approach is not just
an analogy for conceptual purposes: C really does define it that way.
p[1] is -defined- as *(p+1) and p[2] is -defined- as *(p+2) and
p[J] is -defined- as *(p+J) . p+J is J objects further on than p,
not J memory locations further on.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Dec 13 '06 #3

oops!!! I got it and sorry for being lazy with the syntax

Reji

p_***********@yahoo.co.in wrote:
re*********@symantec.com wrote:
Hi,
I have a doubt in the following code:

include <stdio.h>
struct xyz
{
int x;
long l;
float f;
};
int main()

int main (void) is preferred
{
struct xyz xy;
unsigned int i = (&xy + 1)- &xy;
printf( "%u \n",i);
return 0;
}
In the above example &xy cancels each other and the code returns a 1.

Which seems correct.

My question is :
I feel this should give the difference in memory addresses as the
answer (say size of struct (with alignments ))
why doesnt the compiler takes into account the type of xy as pointer
and do a pointer addition. Is this behaviour in conformance with the C
standard or am I missing something??????

Your compiler does take care of pointer math.
Isn't (&xy + 1) at a distance of 1 times (sizeof struct xyz) from &xy ?

Casting pointes to char * might give you the results you expect.

unsigned int i = (char *) (&xy + 1) - (char *)&xy;

- p_cricket_guy
Dec 13 '06 #4
2006-12-13 <el**********@canopus.cc.umanitoba.ca>,
Walter Roberson wrote:
In article <11**********************@j72g2000cwa.googlegroups .com>,
<re*********@symantec.comwrote:
>>I have a doubt in the following code:

"dobut" is not used that way in English,
Misspelling aside, it is self-evident that it is. That it's not used
that way in canadian english is beside the point.
but that's a topic that has been covered in a number of previous
threads.
Right. Is it even worth commenting on anymore?
>
>>struct xyz
{
int x;
long l;
float f;
};
int main()
{
struct xyz xy;
unsigned int i = (&xy + 1)- &xy;
printf( "%u \n",i);
return 0;
}

>>In the above example &xy cancels each other and the code returns a 1.
>>My question is :
I feel this should give the difference in memory addresses as the
answer (say size of struct (with alignments ))
why doesnt the compiler takes into account the type of xy as pointer
and do a pointer addition. Is this behaviour in conformance with the C
standard or am I missing something??????

It is the way that pointer arithmetic is defined.

There is no way in C to get the "memory address" of an object: you can
only get a representation of a pointer to the object. Pointers do not
have to be anything like memory addresses. For example, on DEC's VMS, a
pointer might be the address of a "descriptor" that tells the hardware
information about the type of the object and about the size of the
object, so type-checking and array-bounds checking could be done in
hardware.
Wouln't it be rather more likely for the pointer to be the descriptor
itself?

(AFAIK, the VAX has no such hardware*; there's a reason it's held up as
the example of "typical assumptions" in the old saying "[not] all the
world's a VAX". Perhaps this is done on VMS, but it seems more likely it
would be some low-level piece of software that's doing the checking.
Particularly as we have to take into account pointers to structs of
arbitrary complexity)

*I don't know about the Alpha, but it doesn't seem to either.

On the other hand, the IBM AS/400 is what's most typically held up as an
example of "weird pointers".
Dec 13 '06 #5
re*********@symantec.com wrote:
>
Hi,
I have a doubt in the following code:

struct xyz
{
int x;
long l;
float f;
};
int main()
{
struct xyz xy;
unsigned int i = (&xy + 1)- &xy;
printf( "%u \n",i);
return 0;
}

In the above example &xy cancels each other and the code returns a 1.

My question is :
I feel this should give the difference in memory addresses as the
answer (say size of struct (with alignments ))
why doesnt the compiler takes into account the type of xy as pointer
and do a pointer addition. Is this behaviour in conformance with the C
standard or am I missing something??????
It _does_ "take into account the type of xy as a pointer and do
pointer addition". However, it _also_ takes into account the
types when it does pointer subtraction when you do the "- &xy".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Dec 13 '06 #6
re*********@symantec.com wrote:
>
oops!!! I got it and sorry for being lazy with the syntax
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Dec 13 '06 #7

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

Similar topics

138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
9
by: niclane | last post by:
Hi, I was reading section 5.5 of Ritchie and Kernighan and saw the following: " ..... char amessage = "now is the time"; char *pmessage = "now is the time";
9
by: kiran.agashe | last post by:
Hi, Please refer program below: #include <string> #include <cstdio> using namespace std; const char* f(); main() {
45
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm...
1
by: Michael | last post by:
Hi, My understanding of int op(int x, int y, int (*func)(int,int)). when n = op (20, m, minus); minus is a function pointer, which matches parameter. when m = op (7, 5, addition);there...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
5
by: felix | last post by:
Hi, I am not understanding this particular implementation: /* example.c */ #include <stdio.h> int gl; int *fun ( void )
3
by: venkat | last post by:
Hi, I am learing Unix internals. I have come across a problem where i am not able to understand what is happening. As i gone through the book i found that lseek will give the physical descriptor...
8
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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
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
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...
0
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 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.