• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gasto

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

17 posts in this topic

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
0

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.

0

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.

0

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
0

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.

2

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
1

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

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

Share this post


Link to post
Share on other sites

I initially thought it was a constant pointer, but it seems I can change the address it points to, hence it is not really a constant pointer.

0

Share this post


Link to post
Share on other sites

There are multiple places you can put the const, each with different meanings.

 

"const int * x" means that x is a pointer to constant integers. You can modify the pointer, but you cannot modify what it points to.

 

"int * const x" means that x is a constant pointer to integers. You cannot modify the pointer, but you can modify what it points to.

 

"const int * const x" means x is a constant pointer to constant integers. You cannot modify the pointer, and you cannot modify what it points to.

0

Share this post


Link to post
Share on other sites

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

 I think you meant:

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

Otherwise you would be declaring the function for another overloaded implementation.

 

Nevermind, that was your intention(to declare another function to deal with the function definition with simulated default value.)

Edited by gasto
0

Share this post


Link to post
Share on other sites

 

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

 I think you meant:

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

Otherwise you would be declaring the function for another overloaded implementation.

 

 

No, what I had down was correct.

 

When you provide a default parameter, you are providing a function that takes a parameter. That would be int foo(int). Your function is written with a parameter.

 

If you don't provide a parameter you have an automatically-generated overload without the parameter, that would be int foo(), which is a simple function to call the actual function with that default parameter.

0

Share this post


Link to post
Share on other sites
Try this:
void VectorStuff(const float (&p)[3]={52.0f,53.1f,54.2f});
The problem with the OP is that you can't initialize a pointer with the address of a temporary. Nor can you pass an array by value, using any syntax (except std::array). You can however initialize a const reference with a temporary. You can also use an rvalue reference if you want the array to be mutable.

Note that passing an array like this has one notable downside: you can't dynamically allocate an array like this. You'd have to reinterpret_cast a float * to an float (*) [3]. That's why using std::array is a better solution.

EDIT: The other problem is, you're not using a compiler configured to support C++11 initializer lists, so you're getting a syntax error instead of a semantic error. Edited by King Mir
0

Share this post


Link to post
Share on other sites

There are multiple places you can put the const, each with different meanings.

 

"const int * x" means that x is a pointer to constant integers. You can modify the pointer, but you cannot modify what it points to.

 

"int * const x" means that x is a constant pointer to integers. You cannot modify the pointer, but you can modify what it points to.

 

"const int * const x" means x is a constant pointer to constant integers. You cannot modify the pointer, and you cannot modify what it points to.

 

What I meant with:

 

 

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

 

is that it is not legal syntax to initialize arrays with other arrays or pointers:
 

int array1[3] = {4,5,6};
int array2[3] = array1; // NICHT!
Edited by gasto
0

Share this post


Link to post
Share on other sites

 
What I meant with:

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

is that it is not legal syntax to initialize arrays with other arrays or pointers:
 
int array1[3] = {4,5,6};
int array2[3] = array1; // NICHT!
Ah, that is mostly for historical reasons.

The first one allows the compiler to do some fun trick with data. If the array is constant it is possible for the array to really just be pointer to data inside the program and not allocate any memory at all. If it is non-constant then depending on how it is used will probably need to make a copy of the data, but that is just a memcpy() command.

The second one, however, doesn't work that way. It needs to point array2 to a block of memory somewhere, but the compiler doesn't know exactly where. It could possibly create it on the stack, but then there are some questions about copying it over in such a way that avoids all possibility of aliasing or other problems. While allowing it in some cases like the one you presented are fairly easy, you can construct cases that are very hard or even impossible to figure out at compile time. Things like variable length arrays, partial initialization and incomplete types further complicate the matter.
0

Share this post


Link to post
Share on other sites
Yeah, back in the early days of C, it was decided that it never makes sense to pass an arrays by value. So for uniformity arrays use value syntax just like everything else, but actually are passed as pointers to the underlying type. And you can't assign arrays, or initialize array with other arrays, for similar reasons -- that would be using an array as a value. It's too much implicit logic built to build in to the language.

Today, all those reason are out of date, so std::array exists to make arrays uniform with other types.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0