tags:

views:

268

answers:

6

Hi there, I'm not sure if the term's actually "Array Addition".

I'm trying to understand what does the following line do:

int var[2+1] = {2,1}

How is that different from int var[3]?

I've been using Java for several years, so I'd appreciate if explained using Java-friendly words ;-)

Thanks for your time.

-Nushio

Edit: Thousands of thanks to everyone who helped me out, Occam's Razor applies here.

A: 

How is that different from int var[3]?

In no way that I can see.

James Curran
A: 

It is any different from int var[3]. The compiler will evaluate 2+1 and replace it with 3 during compilation.

Dima
The rest will be default initialized.
dalle
Really? So what's a default for an int? 0?
Dima
Any mssing elements would be 0 filled for POD types.
Martin York
Yes, 0 is default for int. From the standard: "If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized."
dalle
A: 

var[2+1] is not different from var[3]. The author probably wanted to emphasize that var array will hold 2 data items and a terminating zero.

Constantin
A: 

It isn't any different; it is int var[3].

Someone might write their array like this when writing character arrays in order to add space for the terminating 0.

char four[4+1] = "1234";

It doesn't seem to make any sense working with an int array.

Zan Lynx
A: 

This creates an array of 3 integers. You're right, there is no difference whether you express it as 2+1 or 3, as long as the value is constant. The right side of the = is an initializer list and it tells the compiler how to fill the array. The first value is 2, the second 1 and the third is 0 since no more values are specified.

The zero fill only happens when you use an initializer list. Otherwise there is no guarantee of that the array has any particular values.

I've seen this done with char arrays, to emphasize that 1 char is reserved for a string terminator, but never for an int array.

Ferruccio
+7  A: 

It's not different. C++ allows expressions (even non-constant expressions) in the subscripts of array declarations (with some limitations; anything other than the initial subscript on a multi-dimensional array must be constant).

int var[];  // illegal
int var[] = {2,1};  // automatically sized to 2
int var[3] = {2,1};  // equivalent to {2,1,0}: anything not specified is zero
int var[3];  // however, with no initializer, nothing is initialized to zero

Perhaps the code you are reading writes 2+1 instead of 3 as a reminder that a trailing 0 is intentional.

ephemient
As an addendum, char var[]="1234" will give var a size of 5, because a string literal includes the trailing NUL character. One can specify char var[]={'1','2','3','4'} or char var[4]="1234" to overcome this if desired.
ephemient
int var[]; would be allowed as an external declaration - in C for sure, and I believe in C++ too. Not particularly good style - should be in a header, etc - but allowed outside a function/class etc.
Jonathan Leffler
True. I was thinking of stack variables in a function definition: a bare "int var[]" is illegal there, but is permissible in many other situations, such as arguments and external declarations.
ephemient