0

When using scanf for arrays, it seem that the function works fine whether I use '&' operator or not. I want to know how the compiler sees the '&'operator in case of arrays of character

I already know that scanf passes pointer when a single character is used.In case of arrays the variable itself is storing the pointer to first element. My exact question is when I use &ch(while ch is string) do we create double pointer? And how does Compiler operates then

int main() 
{
    char ch,s[100],sen[100];
    scanf("%c",&ch);
    scanf("%s",s);
    scanf(" %[^\n]%*c",sen);

}
kiran Biradar
  • 12,116
  • 3
  • 14
  • 35
  • Note:: Array name decays to pointer. – kiran Biradar Jul 09 '19 at 11:05
  • *"I use &ch(while ch is string) do we create double pointer?"* - No. Applying `&` to a formal non-function variable *always* results in a pointer-to-type, where the type is of the underlying operand. Ex: `int a;` then `&a` is `int *`. Given `char s[N];`, then `&s` is `char (*)[N]`. That's a pointer to and char array of `N` magnitude. It should not be confused with a pointer to `N` chars. They sound the same, but its a "type" thing; they're different. – WhozCraig Jul 09 '19 at 11:14
  • Possible duplicate of [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying) – kiran Biradar Jul 09 '19 at 11:14
  • If you pass `char s[100]` as an argument to a function such as `input(char s[100])` you will discover that `scanf("%s", s)` implemented in the function works, but `scanf("%s", &s)` does not. Now they are very different, in the first case a pointer to the original array is passed to `scanf` but in the second case the address of the pointer *argument* is passed to `scanf`, or pointer to a pointer. – Weather Vane Jul 09 '19 at 11:25

2 Answers2

0

The keyword in this case is array decay. The compiler automatic convert the array to an pointer to the first element of this array.

In your case you have to pass a pointer to scanf. In your first line you pass the address of ch via the & operator. You also can write &Array[0] to point to the first element of your array or you write Array and the compiler will automatic resolve it to &Array[0].

int main()
{
    char Array[1];

    scanf("%c", Array);

    printf("%p\n\r", &Array[0]);
    printf("%p\n\r", Array);

    return 0;
}

Both arguments are the same address. So in case of scanf you use the & operator if you want to get a single integer or a single character from your keyboard:

int value;
scanf("%d", &value);

If you want to receive a char array you pass the array without the & operator and let the compiler do the work for you.

char Array[20];
scanf("%s", Array);
Kampi
  • 1,586
  • 12
  • 23
0

When using scanf for arrays, it seem that the function works fine whether I use '&' operator or not.

"Seems" being the operative word.

Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.

If you call scanf as

scanf( "%s", s );

the expression s is converted from type "100-element array of char" (char [100]) to "pointer to char" (char *) and its value is the address of the first element of the array (&s[0]).

If you call scanf as

scanf( “%s”, &s );

s is the operand of unary &, so the conversion doesn’t occur, and the expression &s has type "pointer to 100-element array of char" (char (*)[100]). This is a problem, since the %s conversion specifier expects an argument of type char *. Passing an argument of any other type results in undefined behavior - the operation may work as expected, or it may crash outright, or it may corrupt data, or it may leave the program in a bad state such that it crashes later, etc.

Both expressions s and &s resolve to the same value - the address of an array is the same as the address of its first element - but their types are different, and in C different pointer types may have different representations (IOW, the way the address value is stored for a char * may be different from the way an address value is stored for a char (*)[100]). So

scanf( "%s", &s );

could fail on some platforms.

My exact question is when I use &ch(while ch is string) do we create double pointer?

No. If ch is declared as char ch, then &ch has type char *. If ch is declared as char ch[N], then &ch has type char (*)[N].

John Bode
  • 106,204
  • 16
  • 103
  • 178