C ++: int x [+30] is a valid declaration?

These days I studied arrays. I met declaring an array and initializing its element in this way:

int x[+30]; x[+1]=0; 

It bothers me a bit. I mean, when we write:

 x[n]=0; 

Then it means:

 *(x+n)=0; 

Then the entry x[+1] will mean *(x++1) , and that seems invalid. Please correct me for the mistake I make in understanding this concept.

+5
source share
2 answers

x[n] means *((x)+(n)) (pay attention to blackets) and x[+1] means *((x)+(+1)) . It's really.

N3337 5.2.1 Subscriptions

The expression E1 [E2] is identical (by definition) to * ((E1) + (E2))

+10
source

The + Plus symbol can act as a unary operator. Usually this has no effect, but the consequence of this is that it is deleted before the number is resolved. For instance:

 int x[+30]; 

Converted to

 int x[operator+(30)]; 

What then becomes

 int x[30]; 

So this expression

 x[+1] = 0; 

It will just be allowed as

 x[1] = 0; 

It will not be resolved as * (x ++ 1), especially since this is an invalid syntax in C ++.

+7
source

Source: https://habr.com/ru/post/1243764/


All Articles