Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default values for array arguments

Just playing around a little with C++. What I really want to do is to be able to setup a function with default values defined for an array or pointer argument. To keep things simple, let's just use an array. Like so:

void experimentA(char a[3] = {'a', 'b', 'c'});

The compiler (LLVM GCC 4.2 with GNU99) complains "Expected expression". That is quite obtuse, but I was told by colleagues that this is happening because the 'value' I'm trying to assign is statically allocated, whereas the variable I'm trying to assign it to (a[3]) is auto.

But I'm not completely sure if that's the case, since I'm able to do this:

void experimentB(char a[3] = "abc");

And the compiler merely warns me that string-literal to char* conversion is deprecated.

I don't understand how "abc" differs fundamentally from {'a','b','c'} in order to cause this discrepancy. Any insight is much appreciated!

like image 256
Dev Kanchen Avatar asked Aug 17 '11 13:08

Dev Kanchen


People also ask

Do the elements in the array have default values?

By default, when we create an array of something in Java all entries will have its default value. For primitive types like int , long , float the default value are zero ( 0 or 0.0 ). For reference types (anything that holds an object in it) will have null as the default value.

What is the default value of an array data type in Java?

Using default values in initialization of arrayFor double or float , the default value is 0.0 , and the default value is null for string.

What are the parameters that are default values?

A parameter with a default value, is often known as an "optional parameter". From the example above, country is an optional parameter and "Norway" is the default value.


3 Answers

Your colleagues are wrong or maybe you misunderstood.

The first clue to understanding is that you cannot have an array as a function parameter in C or C++. The reasons are historical. So when you write void experimentA(char a[3] ...) the compiler automatically converts it to a pointer, i.e. void experimentA(char* a ...). So the real question is why "abc" is a suitable default value for a and { 'a', 'b', 'c' } is not. The reason is as the compiler explains, "abc" is an expression and { 'a', 'b', 'c' } is not (its an initialiser). There are some places in C++ where you can use an initialiser and some where you can't. Default value for a parameter just happens to be one of the places you can't.

like image 138
john Avatar answered Oct 13 '22 13:10

john


When you use string literal "abc", it's allocated by compiler somewhere in the memory, and a pointer to its first character is used as default value. So for the compiler the code is like that: void experimentA(char a[3] = 0x12345678);.

For the second case, the array literal is not allocated by a compiler as a string (which I would see as some inconsistency in the language).

like image 4
Vlad Avatar answered Oct 13 '22 13:10

Vlad


"abc" is an expression. It happens to behave specially, different from all other expressions, when it is used to initialize a variable of type array of char.

{'a','b','c'} is not an expression but an initializer. It is only syntactically allowed in variable definitions. There, the syntax allows either an expression or a non-expression initializer, but that does not man that initializers can be used as expressions anywhere else.

like image 2
hmakholm left over Monica Avatar answered Oct 13 '22 13:10

hmakholm left over Monica