C ++ Is it legal to declare pointers and an array on the same line?

The following code works, but I was told that it does not compile with gcc 3.4.2 with Visual C ++ 2010 and may be illegal:

int ar1[]{0,1,2,3,4,5,6,7,8,9}, *ptr1 = ar1, ar2[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}, *ptr2 = ar2; 

Apparently you need to make some changes to work (something like this):

 int ar1[]{0,1,2,3,4,5,6,7,8,9}; int *ptr1 = ar1; int ar2[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}; int *ptr2 = ar2; 

It is right? Unable to declare arrays and pointers?

(code compiles in QT + gcc 4.8)

+4
source share
1 answer

The declaration in question uses the C ++ 11 initialization syntax. It is not syntactically correct from the point of view of the pre-C ++ 11 compiler. But if you add = before each { , this will become a normal and completely legal C ++ 98 declaration (and declaration C) as well.

There is no problem using multiple declarators in the same declaration, even if you mix pointer and array declarations. If you want, you can add function declarators to this mix. The only limitation is that you cannot embed function definitions there.

+15
source

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


All Articles