Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ * vs [] as a function parameter

What is the difference between:

void foo(item* list)
{
    cout << list[xxx].string;
}

and

void this(item list[])
{
    cout << list[xxx].string;
}

Assuming item is:

struct item
{
    char* string;
}

With the pointer pointing to the first of an array of chars

and list is just an array of items...

like image 204
Flexo1515 Avatar asked May 25 '12 20:05

Flexo1515


People also ask

What is a parameter in C++?

The parameter is referred to as the variables that are defined during a function declaration or definition. These variables are used to receive the arguments that are passed during a function call.

What is the difference between arguments and parameters in C++?

When a function is called, the values that are passed during the call are called as arguments. The values which are defined at the time of the function prototype or definition of the function are called as parameters.

How do you call a function from a parameter?

While calling a function, the arguments can be passed to a function in two ways, Call by value and call by reference. The actual parameter is passed to a function. New memory area created for the passed parameters, can be used only within the function.

How do you pass arguments to a function in C?

While calling a function, the arguments can be passed to a function in two ways, Call by value and call by reference. The actual parameter is passed to a function. New memory area created for the passed parameters, can be used only within the function. The actual parameters cannot be modified here.


1 Answers

To the compiler, there is no difference.

It reads different though. [] suggests you want to pass an array to the function, whereas * could also mean just a simple pointer.

Note that arrays decay to pointers when passed as parameters (in case you didn't already know).

like image 97
Luchian Grigore Avatar answered Oct 11 '22 21:10

Luchian Grigore