Reputation: 26333
what is this syntax for double x[]
?
Is it a C way to declare an array?
If I have a function like
void evaluate(double x[], double *f)
{
// evaluate
}
Can I pass a parameter x with any length?
Upvotes: 0
Views: 370
Reputation: 121407
Inside the parameter list of a function, double x[]
is same as double *x
.
can I pass a parameter x with any length?
Yes, you can pass. But there's no way to know the length of the array x
in the function evaluate
. So you probably want to pass the length as well.
Upvotes: 3
Reputation: 110728
A parameter of array type behaves exactly like it is a pointer. You can not truly pass an array as a function argument. However, this syntax gives the illusion that you can for the sake of readability. So your function is equivalent to:
void evaluate(double *x, double *f)
{
// evaluate
}
From §8.3.5/5 of ISO/IEC 14882:2011:
After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.
An expression that denotes an array will decay to a pointer to its first element, so you can still do this:
void evaluate(double x[]);
int array[] = {1, 2, 3, 4, 5};
evaluate(array); // The array decays to a pointer to its first element
From §4.2:
An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.
So yes, you can indeed pass an array of any length. In reality, you are just passing a pointer to the first element. You will, however, need to pass the length of the array as well, if you need it.
Upvotes: 5
Reputation: 76438
Arrays can be confusing, because in most contexts the name of an array decays into a pointer to its first element. So:
double x[3]; // x is an array of 3 doubles
void f(double x[3]); // f takes a pointer to double
f(x); // calls f with the address of x[0]
The reason for having the array type decay into a pointer for f
is so that you don't have to have a separate function for every array size:
double x[3];
double y[4];
void f(double x[3], int size) {
for (int i = 0; i < size; ++i)
std::cout << x[i] << ' ';
std::cout << '\n';
}
f(x, 3);
f(y, 4);
It works the same way if the argument is double*x
, double x[]
, double x[3]
, double x[17]
, double x[]
; in all of these cases, x
is treated as double*
.
Upvotes: 4
Reputation: 409384
The argument x
to your function is an array of unspecified length. That means that you can pass any array of type double
to it, no matter the size. You can also pass a pointer to a double
as argument.
There is one caveats though: You can't use the sizeof
operator on the array x
, as it doesn't have a size.
Upvotes: 0
Reputation: 14086
Since arrays decay immediately into pointers, an array is never actually passed to a function. You can pretend that a function receives an array as a parameter, and illustrate it by declaring the corresponding parameter as an array:
void f(char a[]) { ... }
Interpreted literally, this declaration would have no use, so the compiler turns around and pretends that you'd written a pointer declaration, since that's what the function will in fact receive:
void f(char *a) { ... }
There's nothing particularly wrong with talking about a function as if it ``receives'' an array, if the function is traditionally used to operate on arrays, or if the parameter is naturally treated within the function as an array.
This conversion of array-like declarators into pointers holds only within function formal parameter declarations, nowhere else. If the conversion bothers you, you're under no compulsion to make use of it; many programmers have concluded that the confusion it causes outweighs the small advantage of having the declaration ``look like'' the call or the uses within the function. (Note that the conversion happens only once; something like char a2[][] won't work. See questions 6.18 and 6.19.)
In other words, in a function parameter declaration, an array with unspecified length is the same as a pointer.
Upvotes: 2
Reputation: 145899
void evaluate(double x[], double *f)
{
// evaluate
}
is actually equivalent to:
void evaluate(double *x, double *f)
{
// evaluate
}
in C and C++.
It means the type of the x
parameter is double *
in both cases.
Upvotes: 1