Array parameter in function prototype not able to receive default value.

Started by
16 comments, last by King Mir 10 years, 1 month ago

void VectorStuff(float p[3]={52.0f,53.1f,54.2f}); // NICHT!

build output:


syntax error : missing ')' before '{'

Functions in C++ are not able to receive default parameters for arrays because It is not really an array parameter, it is a const pointer(like in C.), ergo attempting to assign such parameter with default values on the prototype of the function will confuse the compiler, it will lead the compiler to believe one is attempting to define an array and initialize it in a function parameter list.

Correct me if I am wrong.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
Advertisement

I'm sure there is a question in there waiting to get out.

You could pass the address of a static/global "DefaultParameterArray" if you liked as a default argument. Not sure how useful that would be.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I'm sure there is a question in there waiting to get out.

You could pass the address of a static/global "DefaultParameterArray" if you liked as a default argument. Not sure how useful that would be.

Could you write the pseudo code or C++ code for it? I am not sure what you mean by that.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

int g_DefaultArray[] = { 1, 2, 3, 4 };

void MyFunc(int* arr = g_DefaultArray);

EDIT: Problems there with no passing the size. Use a vector instead.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

int g_DefaultArray[] = { 1, 2, 3, 4 };

void MyFunc(int* arr = g_DefaultArray);

EDIT: Problems there with no passing the size. Use a vector instead.

If you want to restrict the argument to an array of three floats, you can change the parameter type to take a reference to an array instead of a pointer.

void VectorStuff(float (&p)[3] = g_DefaultArray)

Although, the array has to be three elements instead of four, as implied from OP's first post. The size of the array passed is enforced at compile time.

I wrote my reply before Ninja edits added some code to make the question meaningful ;)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Using C++11:

#include <array>

void VectorStuff(std::array<float, 3> const& p = {1,2,3});

Sean Middleditch – Game Systems Engineer – Join my team!

Based on your earlier and revised post, it looks like you are trying to do many things at once. As a general rule, do one thing at a time. You are creating an array, initialized to specific values. Do that in one line. You are calling a function with an array as a parameter. Do that all at once.


You could also be asking about two other kinds of problems.

If you are talking about default parameters, they are not really anything magical.

When you write a function with a default parameter, like so:

int foo( int param = 42 );

what the compiler really does is make a stub function for you that automatically gets inlined, like so:

int foo(int);
inline int foo() { return foo(42); }

You can just as easily do that yourself, and in many pieces of professional code default parameters are forbidden and must be made explicitly by the programmer.

In your case, perhaps:

void VectorStuff( float* ); /* You probably want to make that "const float *" unless you intend to modify values */
inline void VectorStuff() { float p[3]={52.0f,53.1f,54.2f}; VectorStuff(p); )



Or your question might be referring to an unspecified number of parameters, there is something that comes from the ancient past of the language to handle that. It is not recommended because it is unsafe safe, but it might do what you are asking. There is a thing called variadic functions that allows you to pass whatever parameters you want.

Common examples are the printf and scanf family. The declaration of the function is simple enough: int printf ( const char * format, ... ); With that signature you are required to pass a string, and then you can pass zero or more additional parameters. You can then access the parameters passed within the "... "by using the va_xxx family of utilities. It is extremely unsafe for many reasons. Only certain types of data can be passed and there are automatic conversions that take place if your types don't match. It is up to the programmers to ensure the code handles the data types correctly, and there is no automatic mechanism in the language to detect the data type that was passed. If you do it wrong your program will behave badly, crash, or worse. Even though it is unsafe, it is something the language provides and if you are careful can solve some real programming problems.

There is similar functionality in languages like Java and C# that converts the arguments into an array of 'object' type, that is, the base class from which all classes are derived. These languages provide ways to determine the actual type of the data, so their solution is much safer but comes with its own set of hidden costs.

Why is a C array able to receive the address of another C array as an initializer?

float g_array[3]={77.0f,88.0f,99.0f};
 
void VecStuff(float p[3]=g_array);
Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

The argument p in your function is a pointer, and it's default value is to point to the array. The array syntax decays into a pointer, so your function prototype is actually:

void VecStuff(float *p=g_array);

This topic is closed to new replies.

Advertisement