2

I have a Java background and am learning C++. I have a language implementation / syntax decision question.

In Java, array declaration looks like this:

//generally   
type[] variableName;
//concretely
int[] intArray;

However, in C++, it looks like this:

//generally
type variableName[size];
//concretely
int intArray[5];

The Java version makes sense to me, that the brackets should append the datatype. The variable is not an int, it's an int array, so we replace the int with int[].

In C++, why are the brackets after the variable name?

(Further, C++ seems to agree with this when it comes to pointers. An int pointer is int* intPtr not int ptr*. What am I missing?)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Moish
  • 452
  • 1
  • 3
  • 13
  • C declarations in general model the variable's use afterward. You use a pointer via `*p` and you use an array via `arr[i]`. (You can nitpick this because of the relation between those two types, but I hope you see the point.) – chris Sep 20 '21 at 01:41
  • `int[5] arr` might be even wierder because the type of arr might be not `int*`. Besides it doesn't allow `int arr1[5], arr2[10]`. – Nevus Sep 20 '21 at 01:41
  • 2
    I'm out of touch with Java, but doesn't it allow `type variablename[]` as well? – cigien Sep 20 '21 at 01:42
  • 2
    @cigien - yes, but that syntax is old-fashioned (an early error, following the C/C++ syntax), and 'type[] variable' is preferred nowadays. – user16632363 Sep 20 '21 at 01:51
  • 1
    *In c++, why are the brackets after the variable name?* -- Because -- that's the way it is. C++ is not Java. – PaulMcKenzie Sep 20 '21 at 01:54
  • @user16632363 And there you have the explanation. Java designers (while initially supporting C++ syntax) added a syntax that differs from C++. C++ syntax does not evolve just because Java does. The more technical reason is that a Java declaration only defines a reference to an array of unspecified size (i.e. sleight-of-hand in providing something like a C++ pointer that may be dynamically initialised to refer to an array of arbitrary size, while claiming to disallow pointers), whereas an array definition in C++ allocates a collection of objects so the size must be known at compile time. – Peter Sep 20 '21 at 09:56
  • Re sizing: sure; I was really only considering `int[] foo` versus `int foo[]`. But yes, the brackets give a 'property' of the type-of-reference. – user16632363 Sep 20 '21 at 11:54
  • My guess is that when Java was developed, heavily inspired by old C++, they modified some syntax like array declaration in a more logical way. – prapin Sep 20 '21 at 13:34

1 Answers1

7

This is a legacy item from c which declares it in the same way. I think it is likely just how it was done when c was first created. In modern c++ we can avoid using static arrays or pointers. Your example could be written as:

std::array<int, 5> intArray;

which is both more descriptive and adds lots of benefits. See here for more details.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175