C ++ is there an address?

Suppose in C ++ there is the following:

char buffer[SIZE];
char * ptr = &buffer[SIZE];

where the ptrvalue is never dereferenced. Is it legal to do C ++? That is, uses the memory address one step from the last element of the array (say, as a special value for comparison)?

+3
source share
2 answers

If you said:

char buffer[SIZE];
char * ptr = & buffer[SIZE];

then yes, it is legal. You are specifically permitted by the C ++ standard to use one end of the past array in this way, and it is widely used when (for example) working with iterators.

Edit: But see comments from litba and Steve Jessop. If you want to be completely politically correct, you probably want to:

char * ptr = buffer + SIZE;

- , ( ) , .

+10

&buffer[SIZE]? , ++. ptr - , end() iterator .

+6

All Articles