I am a bit confused about the behaviour of a C program from another programmer I am working now with. What I can not understand is the following:
1) a variable is defined this way
typedef float (array3d_i)[3];
array3d_i d_i[NMAX];
2) once some values are assgined to all the d_i's, a function is called which is like this:
void calc(elem3d_i d_element);
which is called from main using:
calc(d_i[i]);
in a loop.
When the d_i's are initialized in main, each element gets an address in memory, I guess in the stack or somewhere else. When we call the function "calc", I would expect that inside the function, a copy of the variable is created, in anoother address. But I debugged the program, and I can see that inside the function "calc", the variable "d_elemt" gets the same address than d_i in main.
Is it normal or not?
I am even more confused because later there is call to another function, very similar situation except that now the variables are float type and also an array 开发者_如何学JAVAof them is initialized, and inside the function, the variables are given a different address than the one in main.
How can this be? Why the difference? Is the code or the debugger doing something weird?
Thanks
Arrays are passed by reference in C, while simple values will be passed by value. Or, rather, arrays are also passed by value, but the "value" of an array in this context is a reference to its first element. This is the "decay" Charles refers to in his comment.
By "reference", I mean pointer of course since C doesn't have references like C++ does.
C does not have a higher-level array concept, which is also why you can't compute the length of the array in the called function.
It's the difference between pointers and variables. When you pass an array to a function you are passing a pointer (by value). When you pass a float, you are passing a float (by value). It's all pass by value, but with the array the value is the address of the pointer.
Note that "passing arrays" is the same as passing pointers and that, in C
all parameters are passed by value.
What you see in the different functions is the pointer value. The pointer itself (the parameter received by the function) is a different one in each function (you can check its address)
Imagine
int a = 42, b = 42, c = 42;
If you look at a
, b
, or c
in the debugger you always see 42 (the value), but they're different variables.
As others have noted, everything is passed by value. However, that can be misleading for arrays. While similar to pointers, arrays are not pointers. Pointers are variables which hold addresses. Arrays are blocks of memory, at a particular address. For arrays, there is no separate variable which holds the address, like a pointer. When you pass an array as an argument to a function, or get it's address by assigning the name (without the [] indexing) to a pointer, then you do have it's address contained in a variable. So, what is "passed by value" is a pointer, not an array, even though you called the function with an array as an argument. So the following are equivalent:
void func1(char *const arg);
void func2(char arg[]);
精彩评论