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

后端 未结 5 1816
情深已故
情深已故 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:42

    • All the variables used to recieve values through scanf() must be passed by their addresses. This means that all arguments must be pointed to the variables used as arguments.

      scanf("%d", &count);

    • Stings are read into character arrays, and the array name without any index is the address of the first element of the array.So to read a string into a character array address, we use

      scanf("%s",address);

    • In this case address is already a pointer and need not to be preceded by the & operator.

提交回复
热议问题