Why it is possible to assign string to character pointer in C but not an integer value to an integer pointer

后端 未结 3 1794
闹比i
闹比i 2021-01-19 14:56

why in the below code int *p = 22 will give compile time error and ptr will print the value successfully .

int main()
{

/*taking a character pointer and ass         


        
3条回答
  •  面向向阳花
    2021-01-19 15:31

    Because the string is actually an array of char's and when assigning an array to a variable, the arrays will behave as a pointer to the first element of the array. This can be confusing. More on this here:

    when does a array act as a pointer in c?

    The integer on the other hand is just an integer, not an array of integers.

提交回复
热议问题