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

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

    When you use the name of an array in an expression (except as the operand of sizeof or the address-of operator &), it will evaluate to the address of the first item in that array -- i.e., a pointer value. That means no & is needed to get the address.

    When you use an int (or short, long, char, float, double, etc.) in an expression (again, except as the operand of sizeof or &) it evaluates to the value of that object. To get the address (i.e., a pointer value) you need to use the & to take the address.

提交回复
热议问题