Why does scanf() need & operator (address-of) in some cases, and not others?

后端 未结 5 1804
情深已故
情深已故 2020-11-28 13:29

Why do we need to put a & operator in scanf() for storing values in an integer array but not while storing a string in a char array?

         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 13:41

    scanf("%d", a + i ) works too.

    %d and %s just tell scanf what to expect but in both cases it expects an address

    in C, arrays and pointers are related.

    %s just says to scanf to expect a string which is \0 terminated, whether it will fit into the character array or not scanf doesn't care.

提交回复
热议问题