473,378 Members | 1,396 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,378 software developers and data experts.

increment pointer

Hi,
I work on a little endian machine.
i have an array int a[]={2,3,4};
i have a pointer int *p;
p=a;
now when i printf(" %x",p);
i got 0xff43n (where n is some value)
now p++
i get 0xff43n+4 (where n+4 is the 4 bytes).
I work on 32 bit machine.

Now my question is that if i do p++ , will it be 0xff43n+4 on little
endian and 0xff43n -4 on big endian.

I think no. It should be the same on the both i mean it increments the
base address by size of work. Am i correct or wrong.

Regards
Nik

May 29 '06 #1
8 3309
<te********@gmail.com> schrieb im Newsbeitrag
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi,
I work on a little endian machine.
i have an array int a[]={2,3,4};
i have a pointer int *p;
p=a;
now when i printf(" %x",p);
i got 0xff43n (where n is some value)
now p++
i get 0xff43n+4 (where n+4 is the 4 bytes).
I work on 32 bit machine.

Now my question is that if i do p++ , will it be 0xff43n+4 on little
endian and 0xff43n -4 on big endian.

I think no. It should be the same on the both i mean it increments the
base address by size of work. Am i correct or wrong.


On most machines I would expect (int) p to be less than (int)(p + 1), at
least if int is large enough to hold a pointer. But I don't remember that
the standard does require it. Of cause p < (p + 1) or (p+1) - p == 1 should
always be true, but that does not really imply that this should also be true
if you interpret the bit patterns of a pointer as integers. But that
shouldn't really matter. If your program relies on such lowest level
implementation details, you must be doing something really special -- or
wrong.

Regards
Heinz

May 29 '06 #2
In article <11*********************@g10g2000cwb.googlegroups. com>,
<te********@gmail.com> wrote:
I work on a little endian machine.
i have an array int a[]={2,3,4};
i have a pointer int *p;
p=a;
now when i printf(" %x",p);
i got 0xff43n (where n is some value)
now p++
i get 0xff43n+4 (where n+4 is the 4 bytes).
I work on 32 bit machine. Now my question is that if i do p++ , will it be 0xff43n+4 on little
endian and 0xff43n -4 on big endian.


No, but for a completely different reason. %x is not the proper
format to use to print out a pointer, so when you move to a
different machine, you might get any of a variety of results.
--
Programming is what happens while you're busy making other plans.
May 29 '06 #3
In article <44***********************@newsread4.arcor-online.net>,
Heinz Ozwirk <ho**********@arcor.de> wrote:
On most machines I would expect (int) p to be less than (int)(p + 1), at
least if int is large enough to hold a pointer. But I don't remember that
the standard does require it.


I thought I'd posted a proof of this recently, but I cannot find
the appropriate posting now.

- C requires that addresses within structures are increasing
- one could union an array[10] together with a structure that
was two array[5]
- the second array within the struct must an increased address
relative to the first, due to the first point
- the address of the union would be common base -- which would
be the address of the structure and the address of the array[10]
- thus the address of the array[10] must be the address of the
first array[5] of the struct
- I cannot think of any logical way that after that the address of
the array[10] at index 5 would not be the same as the address
of index 0 of the second array[5]
- So since that second array[5] has an increased address relative
to the first, then the address of the array[10] at index 5 must
be increased in memory relative to that array at index 0, so
arrays must increase in memory
--
All is vanity. -- Ecclesiastes
May 29 '06 #4
te********@gmail.com writes:
Hi,
I work on a little endian machine.
i have an array int a[]={2,3,4};
i have a pointer int *p;
p=a;
now when i printf(" %x",p);

[...]

You've just invoked undefined behavior.

To print a pointer value, use %p:

printf(" %p", (void*)p);

Incidentally, cross-posting to comp.lang.c and comp.lang.c++ is rarely
a good idea. In C++, something involving "cout << ..." might be a
preferred solution. If you want to discuss that in more detail,
please drop comp.lang.c.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 29 '06 #5
te********@gmail.com wrote:
Hi,
I work on a little endian machine.
i have an array int a[]={2,3,4};
i have a pointer int *p;
p=a;
now when i printf(" %x",p);
i got 0xff43n (where n is some value)
now p++
i get 0xff43n+4 (where n+4 is the 4 bytes).
I work on 32 bit machine.

Now my question is that if i do p++ , will it be 0xff43n+4 on little
endian and 0xff43n -4 on big endian.

I think no. It should be the same on the both i mean it increments the
base address by size of work. Am i correct or wrong.

Regards
Nik

Endianess has nothing to do with your observations. On your machine..

(sizeof (int) == 4) is true.

Expressing a yields a value of type (int *) which is perfect for..

int *p = a;

The value of p points to the first int in the array such that *p == 2.
If you would then increment p (++p) then *p == 3 will be true.

Endianess has nothing to do with it.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
May 29 '06 #6
On 2006-05-29 23:21, Walter Roberson wrote:
In article <44***********************@newsread4.arcor-online.net>,
Heinz Ozwirk <ho**********@arcor.de> wrote:
On most machines I would expect (int) p to be less than (int)(p + 1), at
least if int is large enough to hold a pointer. But I don't remember that
the standard does require it.
I thought I'd posted a proof of this recently, but I cannot find
the appropriate posting now.

- C requires that addresses within structures are increasing
- one could union an array[10] together with a structure that
was two array[5]
- the second array within the struct must an increased address
relative to the first, due to the first point
- the address of the union would be common base -- which would
be the address of the structure and the address of the array[10]
- thus the address of the array[10] must be the address of the
first array[5] of the struct
- I cannot think of any logical way that after that the address of
the array[10] at index 5 would not be the same as the address
of index 0 of the second array[5]


I'm not 100% sure but I believe that the compiler is allowed to add
padding between the two array[5]. An example would be if you used arrays
of char (one byte per element) and the architecture aligns all data-
structures on 4-byte boundaries, which would mean that there would be 3
bytes padding between the arrays.

This of course does not have any direct bearing on your reasoning but
illustrates the danger of doing cleaver things with unions.
- So since that second array[5] has an increased address relative
to the first, then the address of the array[10] at index 5 must
be increased in memory relative to that array at index 0, so
arrays must increase in memory


Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
May 29 '06 #7
Erik Wikström posted:

I'm not 100% sure but I believe that the compiler is allowed to add
padding between the two array[5]. An example would be if you used
arrays of char (one byte per element) and the architecture aligns all
data- structures on 4-byte boundaries, which would mean that there
would be 3 bytes padding between the arrays.

This of course does not have any direct bearing on your reasoning but
illustrates the danger of doing cleaver things with unions.
- So since that second array[5] has an increased address relative
to the first, then the address of the array[10] at index 5 must
be increased in memory relative to that array at index 0, so
arrays must increase in memory


Erik Wikström

Good catch. There's a recent discussion about this on comp.std.c++.

union Monkey {

array1[10];

struct {
array2[5];
array3[5];
};

};

int main()
{
Monkey obj;

assert( (obj.array1 + 5) == (obj.array3) );
}
This is well within its rights to cause an assertion failure.
-Tomás
May 29 '06 #8
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <44***********************@newsread4.arcor-online.net>,
Heinz Ozwirk <ho**********@arcor.de> wrote:
On most machines I would expect (int) p to be less than (int)(p + 1), at
least if int is large enough to hold a pointer. But I don't remember that
the standard does require it.


I thought I'd posted a proof of this recently, but I cannot find
the appropriate posting now.

- C requires that addresses within structures are increasing
- one could union an array[10] together with a structure that
was two array[5]
- the second array within the struct must an increased address
relative to the first, due to the first point
- the address of the union would be common base -- which would
be the address of the structure and the address of the array[10]
- thus the address of the array[10] must be the address of the
first array[5] of the struct
- I cannot think of any logical way that after that the address of
the array[10] at index 5 would not be the same as the address
of index 0 of the second array[5]
- So since that second array[5] has an increased address relative
to the first, then the address of the array[10] at index 5 must
be increased in memory relative to that array at index 0, so
arrays must increase in memory


All of this is true, but it merely demonstrates that p must compare
smaller than p+1, not that (int)p must compare smaller than (int)p+1.

Richard
May 30 '06 #9

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

Similar topics

21
by: deepak | last post by:
hi folks I have a pointer to a file. If i increment the pointer, where will it point. I could not find the problem in the FAQs. Thanks in advance Deepak garg
15
by: ranjeet.gupta | last post by:
Dear All, Which is correct ? Let suppose we allocate the memory for the 100 bytes, int *PtrMemoryBlock = (int *)malloc(100); If I do
14
by: Robben | last post by:
Hi All, I have an integer pointer with value 10, but it returns a different return value after the preincrement and post increment. I will be very thankful if somebody can give an explanation...
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
2
by: braton | last post by:
Hello I'm wondering about the following thing: Let's define variable and set a pointer to points to it: int a = 0; int * p_a = &a; Now, according to C99, clause 6.5.6.7 I can treat...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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 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.