C Programming - Step-by-Step Instructions

In the C program below, I do not understand why buf [0] = 'A' after calling foo. Doesn't foo do pass-by-value?

#include <stdio.h> #include <stdlib.h> void foo(char buf[]) { buf[0] = 'A'; } int main(int argc, char *argv[]) { char buf[10]; buf[0] = 'B'; printf("before foo | buf[0] = %c\n", buf[0]); foo(buf); printf("after foo | buf[0] = %c\n", buf[0]); system("PAUSE"); return 0; } 

output:

 before foo | buf[0] = 'B' after foo | buf[0] = 'A' 
+6
c pass-by-value
source share
12 answers
 void foo(char buf[]) 

coincides with

 void foo(char* buf) 

When you call it, foo(buf) , you pass a pointer by value, so a copy of the pointer is created.

A copy of the pointer points to the same object as the source pointer (or, in this case, the source element of the array).

C does not have a pass on reference semantics in the sense that C ++ passes on reference semantics. Everything in C is passed by value. Pointers are used to get pass on referential semantics.

+12
source share

an array is just a fancy way of using a pointer. When you pass buf function, you pass a pointer by value, but when you cast a pointer, you still refer to the string that it points to.

+4
source share

An array as a function parameter is equivalent to a pointer, so the declaration

 void foo( char buf[] ); 

coincides with

 void foo( char* buf ); 

The array argument then decayed points to its first element.

+2
source share

Arrays are processed differently than other types; you cannot pass an array "by value" to C.

C99 Online Standard (Project n1256) , Section 6.3.2.1, β€œLvalues, Arrays, and Functions,” clause 3:

Unless it is an operand of the sizeof operator or a unary operator and the operator or string literal used to initialize the array, an expression that is of type '' array of type is converted to an expression with type '' pointer to type pointing to the starting element of the object array and is not an lvalue value. If the array object has a register storage class, the behavior is undefined.

In a call

 foo(buf); 

the buf array expression is not an operand of sizeof or & , and is not a string literal used to initialize the array, therefore it is implicitly converted ("decays") from the type "10- array of char elements" to "pointer to char", and the address of the first element passed to foo. Therefore, everything you do with buf in foo() will be reflected in the buf array in main() . Due to how the indexing of an array is determined, you can use the index operator by type of pointer to make it look like you are working with an array type, but it is not.

In the context of declaring a function parameter, T a[] and T a[N] are synonyms with T *a , but this is the only case where this is true.

+2
source share

* char buf [] actually means char **, so you pass a pointer / link. This gives you that buf is a pointer, both in the main () and foo () functions.

+1
source share

Because you are passing a pointer to buf (by value). Thus, the contents pointed to by buf change.

+1
source share

With pointers, it is different; you pass by value, but what you pass is a pointer value that does not match the value of the array.

So, the value of the pointer does not change, but you change what it points to.

+1
source share

arrays and pointers are (almost) the same thing.

 int* foo = malloc(...) 

foo[2] matches *(foo+2*sizeof(int))

joke: you wrote

 int main(int argc, char *argv[]) 

it is also legal (will compile and work the same way) for recording

 int main(int argc, char **argv) 

and

 int main(int argc, char argv[][]) 

they are practically the same. its a bit more complicated because the array knows how many elements it has, but the pointer doesn't. but they are used the same way.

+1
source share

to pass this value, the function must know the size of the argument. In this case, you just pass the pointer.

0
source share

You follow the link here. In this example, you can solve the problem by passing one char to the index of the desired array.

If you want to save the contents of the original array, you can copy the string to temporary storage in this function.

edit: What happens if you wrap the char array in a structure and pass the structure? I believe this can work, too, although I don’t know what overhead might occur at the compiler level.

0
source share

note one thing

declaration

 void foo(char buf[]) 

says it will use [] notation. Not the element of the array that you will use.

if you want to indicate that you want a specific value, then you must declare this function as

 void foo(char buf[X]); //where X would be a constant. 

Of course, this is impossible, because it would be useless (function to work in the nth element of the array?). You do not need to write down information about which element of the array you want to receive. All you need is a simple declaration:

 voi foo(char value); 

So...

 void foo(char buf[]) 

- This is an ad that indicates which notation you want to use ([] - part), and also contains a pointer to some data.

Moreover ... what would you expect ... you sent the function foo the name of the array

 foo(buf); 

which is equivalent to & buf [0]. So ... this is a pointer.

0
source share

Arrays in C are not passed by value. They are not even legitimate function parameters. Instead, the compiler sees that you are trying to pass an array and lowers it to a pointer. He does it quietly because it is evil. He also loves to drink puppies.

Using arrays in function parameters is a good way to tell API users that this thing should be a block of memory segmented into n-byte chunks, but don't expect compilers to care if you say char *foo char foo[] or char foo[12] in the functional parameters. They will not.

0
source share

All Articles