Reputation: 99428
when I need to pass an array to a function, it seems all the following declarations of the function will work
void f(int arr[])
void f(int arr[4]) // is this one correct?
for this:
int a[]={1,2,3,4};
f(a);
But when I assign an array to another array, it fails
int a[]={1,2,3,4};
int b[4] = a; // error: array must be initialized with a brace-enclosed initializer
So why an array passed as an argument of a function is okay, but used on the rhs of simple assignment is wrong?
Upvotes: 6
Views: 3876
Reputation: 379
I want to clarify. There are some misleading hints in answers... All following functions can take integer arrays:
void f(int arr[])
void f(int arr[4])
void f(int *arr)
But formal arguments are not the same. So the compiler can handle them differently. In the sense of internal memory management, all arguments lead to pointers.
void f(int arr[])
... f() takes an array of any size.
void f(int arr[4])
... The formal argument indicates an array size.
void f(int *arr)
... You can also pass an integer pointer. f() does not know anything about the size.
Upvotes: 0
Reputation: 18446
Note that the type of a
in int a[4]
is int [4]
.
But TypeOf(&a
) == int (*)[4]
!= int [4]
.
Also note that the type of the value of a
is int *
, which is distinct from all of the above!
Here's a sample program you can try:
int main() {
// All of these are different, incompatible types!
printf("%d\n", sizeof (int[4])); // 16
// These two may be the same size, but are *not* interchangeable!
printf("%d\n", sizeof (int (*)[4])); // 4
printf("%d\n", sizeof (int *)); // 4
}
Upvotes: 0
Reputation: 96151
For understanding the difference, we need to understand two different contexts.
T
is equivalent to a pointer to type T
, and is equal to a pointer to the array's first element.T
does not reduce to a pointer.What is object context?
In a = b;
, a
is in object context. When you taken the address of a variable, it's used in object context. Finally, when you use sizeof
operator on a variable, it's used in object context. In all other cases, a variable is used in value context.
Now that we have this knowledge, when we do:
void f(int arr[4]);
It is exactly equivalent to
void f(int *arr);
As you found out, we can omit the size (4 above) from the function declaration. This means that you can't know the size of the "array" passed to f()
. Later, when you do:
int a[]={1,2,3,4};
f(a);
In the function call, the name a
is in value context, so it reduces to a pointer to int
. This is good, because f
expects a pointer to an int
, so the function definition and use match. What is passed to f()
is the pointer to the first element of a
(&a[0]
).
In the case of
int a[]={1,2,3,4};
int b[4] = a;
The name b
is used in a object context, and does not reduce to a pointer. (Incidentally, a
here is in a value context, and reduces to a pointer.)
Now, int b[4];
assigns storage worth of 4 int
s and gives the name b
to it. a
was also assigned similar storage. So, in effect, the above assignment means, "I want to make the storage location the same as the previous location". This doesn't make sense.
If you want to copy the contents of a
into b
, then you could do:
#include <string.h>
int b[4];
memcpy(b, a, sizeof b);
Or, if you wanted a pointer b
that pointed to a
:
int *b = a;
Here, a
is in value context, and reduces to a pointer to int
, so we can assign a
to an int *
.
Finally, when initializing an array, you can assign to it explicit values:
int a[] = {1, 2, 3, 4};
Here, a has 4 elements, initialized to 1, 2, 3, and 4. You could also do:
int a[4] = {1, 2, 3, 4};
If there are fewer elements in the list than the number of elements in the array, then the rest of the values are taken to be 0:
int a[4] = {1, 2};
sets a[2]
and a[3]
to 0.
Upvotes: 12
Reputation: 5967
Try memcpy.
int a[]={1,2,3,4};
int b[4];
memcpy(b, a, sizeof(b));
Thanks for pointing that out, Steve, it's been a while since I used C.
Upvotes: 3
Reputation: 19114
To get your intuition about it, you must understand what's going on on machine level.
The initialization semantics (= {1,2,3,4}) means "put it on your binary image exactly this way" so this can be compiled.
Array assignment would be different: the compiler would have to translate it into a loop, which would actually iterate over elements. C compiler (or C++, for that matter) never does such a thing. It rightfully expects you to do it yourself. Why? Because you can. So, it should be a subroutine, written in C (memcpy). This is all about simplicity and closeness to your weapon, which is C and C++ all about.
Upvotes: 1
Reputation:
C does not support the assignment of arrays. In the case of a function call, the array decays to a pointer. C does support the assignment of pointers. This is asked here just about every day - what C text book are you guys reading that doesn't explain this?
Upvotes: 6
Reputation: 47052
void f(int arr[]);
void f(int arr[4]);
The syntax is misleading. They are both the same as this:
void f(int *arr);
i.e., you are passing a pointer to the start of the array. You are not copying the array.
Upvotes: 7