Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function prototype and array parameters

I'm learning C++ syntax and I got to the point where I'm looking into arrays. I would like to ask you a question but first let me recap so I know that I got this stuff straight. I know that you can define a variable as an array with the following syntax:

<base type> name [<size(constexpr)>]

(the size being part of the type of the array). This would give me an array of size elements of base type If I wanted an array of pointers to the base type I could just add a * after the base type specifier as in normal pointer declaration.

<base type> *name [<size(constexpr)>]

I can't define an array of references because aray are supposed to hold only objects ( and references are just aliases).

Now, if I wanted to declare a reference or a pointer to an array I could use this syntax:

<base type> (&name) [<size(constexpr)>]

or

<base type> (*name) [<size(constexpr)>]

So far it is all clear. What I also know, is that I can pass an array as an argument to a function, but that call will always be interpreted as I am passing a pointer to the array's elements type. A function that is declared as follows:

void f(int array[10])

is the same as:

void f(int array[])
void f(int *p)

and whenever I call this function I am always passing a int* (passing by value).

Question: what if I wanted to write the function prototype (a pure declaration) without using the parameter's name? I know that I can ordinarily omit parameter names in that case (I could write void f(int*) but what about the other two declarations?).And more importantly, what if the parameter is a reference or a pointer to an array?

void f(int (&array)[])

or

void f(int (*array)[])

thanks!

like image 912
Luca Avatar asked Jul 29 '15 10:07

Luca


People also ask

What is an array parameter?

A parameter array can be used to pass an array of arguments to a procedure. You don't have to know the number of elements in the array when you define the procedure. You use the ParamArray keyword to denote a parameter array.

Can a function take an array as a parameter?

In this tutorial, we will learn how to pass a single-dimensional and multidimensional array as a function parameter in C++ with the help of examples. In C++, we can pass arrays as an argument to a function. And, also we can return arrays from a function.

WHAT IS function and its prototype?

A function prototype is a definition that is used to perform type checking on function calls when the EGL system code does not have access to the function itself. A function prototype begins with the keyword function, then lists the function name, its parameters (if any), and return value (if any).

How can you pass an array to a function?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.


1 Answers

This is perfectly fine:

void f(int[])
void f(int*)

This is as well:

void f(int (*)[])

Pointer is a pointer. In this case it points to an array, but it is still a pointer.

However, this is not allowed:

void f(int (&)[])

No matter if name is specified or not. This declares parameter as "reference to array of an unknown bound", which is not allowed. If you want to receive reference to array of an unknown size, you can use templates for that:

template <size_t N>
void f(int (&array)[N]);

It is perfectly valid. Of course, fixed-size array (sized at compile time) can also appear without a name:

void f(int (&)[32]) //Ok
like image 176
Mateusz Grzejek Avatar answered Oct 28 '22 05:10

Mateusz Grzejek