Arrays in c are always passed by reference and printf is a call by value function then why do we pass array to printf function for printing a string?
Asked
Active
Viewed 626 times
1
-
1C pass always by value, including arrays, see this post http://stackoverflow.com/questions/2229498/passing-by-reference-in-c – terence hill Apr 04 '16 at 13:05
2 Answers
5
First, arrays are not passed by reference in C. They decay to a pointer, which is passed by value to the function.
There is no pass by reference in C. It can only be emulated with pass by value.
We don't really pass arrays to printf
: it's the aforementioned pointers we obtain through an implicit conversion (decay). This pointer can just as well be dereferenced to access the content of the array, so passing an array "by value" is not needed at all.

cadaniluk
- 15,027
- 2
- 39
- 67
-
But we do not write *(name_of_array) as an argument to printf, we simply write the name of array. I mean we write printf("%s",name_of_array) instead of printf("%s",*name_of_array) then how come(given that array is decaying to pointer) is it derefrenced? – user6112826 Apr 04 '16 at 13:12
-
@user6112826 The array is passed as a pointer. The function's parameter for that array is then `T* ptr` where `T` is an arbitrary type. The function can dereference that pointer easily by doing `ptr[N]` where `N` is an arbitrary integral expression like `5` or `i + 2`. Just look at a simply `printf` implementation and you'll understand. – cadaniluk Apr 04 '16 at 13:18
-
@user6112826: Unless it is the operand of the `sizeof` or unary `&` operators, the compiler will treat `name_of_array` as a pointer to the first element of the array. See [this answer](http://stackoverflow.com/questions/4607128/in-c-are-arrays-pointers-or-used-as-pointers/4608421#4608421) for a more detailed explanation. – John Bode Apr 04 '16 at 13:45
-
@JohnBode I know about array decaying, what i was asking is that if contents of arrays are to be copied then we must derefrence it. As name of array is alias to the address of first element of array then should we not derefrence to pass the contents? Because simply passing array would pass the address as it decays to pointer – user6112826 Apr 04 '16 at 13:49
-
@user6112826: "As name of array is alias to the address of first element of array ..." - The prerequisite is already wrong. "decays" (aka "is implicitly converted") does not mean "is identical to". No offence, but please understand stack overflow is not a tutoring site. If the comments and answers here or other related questions still don't help, you should start reading a good C book (there are some for free to be found) from the very beginning. This is very basic stuff about arrays and pointers. Any way, you will not get the picture by asking particular questions. – too honest for this site Apr 04 '16 at 14:08
-
@user6112826: The `%s` conversion specifier expects its corresponding argument to be a pointer value (`char *`); given that address value, it will "walk" down the array and print each character in turn until it sees the 0 terminator. The function accesses the array's contents via that pointer value. If you dereference the array name in the argument list, you will wind up passing a single character value, not a sequence of characters. – John Bode Apr 04 '16 at 14:16
-
"They _decay_ to a pointer," Never saw _decay_ defined in a C spec. Where is _decay_ defined? – chux - Reinstate Monica Apr 04 '16 at 14:51
-
@chux In case you're not being ironic: the term is in widespread use despite the fact it's not described in the standard and denotes the implicit conversion from `T[]` to `T*`. – cadaniluk Apr 04 '16 at 17:58
0
You don't pass arrays to printf, instead you pass the values in the array.
int arr[] = {1,2,3};
printf("%d",arr); //Undefined behavior(garbage value)
printf("%d",arr[0]); //will print 1
Also you can pass a pointer as an argument to a function but not a reference in C.
Edit
In case of char*
which is pointer to char ,all characters in the array can be displayed till the NULL character is reached using
char* str = "Hello";
printf("%s",str);

XZ6H
- 1,779
- 19
- 25
-
-
It is possible if you create an array of characters and use %s format specifier for printing string. – user6112826 Apr 04 '16 at 13:14
-
-
@FiddlingBits: Your comment is not clear respecting OPs apparent beginner level. I suspect you mean _string literals_. But they actually _don't_ make a difference. – too honest for this site Apr 04 '16 at 13:16
-
@user6112826: C does not have a string type. It is all convention: a `char []` with `'\0'` terminator. – too honest for this site Apr 04 '16 at 13:17
-
I wasn't specific enough. What I meant was a c-string and the `%s` format specifier are a special case where you pass the address of the array to `printf` and the contents are printed, not the address. – Fiddling Bits Apr 04 '16 at 13:23
-
@Fiddling Bits you mean there is an implicit dereferencing of the array in this case? – user6112826 Apr 04 '16 at 13:30
-
@FiddlingBits: Ok, but that does not make the "string" different, but just how the `%s` type specifier is interpreted by `printf`. (Sorry for nit-picking, but OP is already confused enough:-) – too honest for this site Apr 04 '16 at 13:36
-
The correct format specifier for printing memory addresses is `%p`, not `%d`. You also need to cast its argument to `void*`. – Spikatrix Apr 04 '16 at 14:26
-
-
@XYZProgrammer Could be anything due to Undefined Behavior as per the C standards. Use `printf("%p", (void*)arr);` instead. – Spikatrix Apr 04 '16 at 14:58