while scanning string we won't use ambers '&' why??

Recommended Answers

All 2 Replies

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] ...)

In case of arrays the name of the array itself behaves as the pointer, like:
if declaration is int array[10];
then,
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...;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.