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

address after end of array?

Is this correct code?

int array[10];

int *start = array;
int *end = &array[9];

while (start <= end) start++;

Oct 23 '06 #1
5 1790
Serve Laurijssen said:
Is this correct code?

int array[10];

int *start = array;
int *end = &array[9];

while (start <= end) start++;
Yes. The loop terminates when start is pointing ***just*** past the end of
the array. This is legal, provided you don't go any further than that, and
provided you don't dereference. And no, you can't go off the beginning of
the array in the same manner - *that* is illegal.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 23 '06 #2
"Serve Laurijssen" <se*@n.tkwrites:
Is this correct code?

int array[10];

int *start = array;
int *end = &array[9];

while (start <= end) start++;
Yes. Note that you can't dereference start after the loop, you can't go
further and you can't go before the start of the array.

Yours,

--
Jean-Marc
Oct 23 '06 #3
Richard Heathfield posted:
Yes. The loop terminates when start is pointing ***just*** past the end of
the array. This is legal, provided you don't go any further than that, and
provided you don't dereference.

Incidently, the following constitutes a dereference:

int array[N];

int *p = &array[N];

, and thus its behaviour is undefined. The following, however is OK in its
place:

int *p = array + N;

(I have a feeling that this was relaxed in one of the newer standards...)

--

Frederick Gotham
Oct 23 '06 #4
Frederick Gotham wrote:
Richard Heathfield posted:
>Yes. The loop terminates when start is pointing ***just*** past the end of
the array. This is legal, provided you don't go any further than that, and
provided you don't dereference.


Incidently, the following constitutes a dereference:
Not when it's the operand of unary &.

int array[N];

int *p = &array[N];
Oct 24 '06 #5
Nils O. Selåsdal wrote:
Frederick Gotham wrote:
Richard Heathfield posted:
Yes. The loop terminates when start is pointing ***just*** past the end of
the array. This is legal, provided you don't go any further than that,and
provided you don't dereference.

Incidently, the following constitutes a dereference:
Not when it's the operand of unary &.
When it's the operand of unary &, compilers in practice don't have
problems with it, the meaning is obvious, and as suspected by Frederick
Gotham in the message you replied to, it's allowed in C99. However,
evaluation of array[N] is disallowed in all versions of C, and unless
C90 specifically says array[N] is not evaluated when it is the operand
of the address-of operator (wording which I believe is new to C99), the
behaviour is undefined in C90.
int array[N];

int *p = &array[N];
Oct 24 '06 #6

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

Similar topics

8
by: Timo | last post by:
I am trying to get address of myStruct to a string array texts. I am using M$ Visual C++ 6.0 (this is not OS specific question, though, this code should also work on 16 bit embedded compiler ;)). ...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
0
by: comp.lang.php | last post by:
I wrote a method that should check if an email address is valid. In another method I've already checked to see if $_POST exists and is well-formed, so those checks are not necessary in this scope....
10
by: | last post by:
If I have an array of int: int array; I suppose the correct way to clear it using memset() would be: memset(array, 0, 8 * sizeof(int)); However, I've seen the following in a piece of code:...
8
by: =?big5?B?r0W84Q==?= | last post by:
Hi All C gurus: Below is a small program to print out the address of array and address of array variable: int main() { char array = "haha"; printf("array:%x\n", array); printf("&array:%x\n",...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.