Jump to content
  • Advertisement
Sign in to follow this  
gasto

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

This topic is 2057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by gasto

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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. Edited by frob

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!