6 Years
Discussion Span
Last Post by kings_mitra

The compiler will generate a pointer to the first character of a character array if all you pass is the array name, The & symbol is optional. If you want a pointer somewhere else in the array then you have to use the & operator and tell it which byte you want the pointer to start.
char buf[255];
scanf(buf, ...);

The above line is the same as this one:
scanf(&buf[0] ...)

Edited by Ancient Dragon: n/a


In case of arrays the name of the array itself behaves as the pointer, like:
if declaration is int array[10];
array = address of first element
&array[0] = address of first element
&array = address of whole array

But what will happen if you declare a function that has an argument whose type is an array type — like this:
void function(int array[10]);
The answer might suprise us...
The compiler looks at that and thinks, that it is going to be a pointer when the function is called and then rewrites the parameter type to be a pointer.
As a result, all the following three types of these declarations are identical:

void function(int array[10]);
void function(int *array);
void function(int array[]);       /* since the size of the array is irrelevant! */

Thats why programming is so interesting if you understand the insights...;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.