小编Div*_*ash的帖子

为什么可以将字符串分配给C中的字符指针,但不能将整数值分配给整数指针

为什么在下面的代码中 int *p = 22 会给出编译时错误,而 ptr 会成功打印该值。

int main()
{

/*taking a character pointer and assigning a string to it*/
char *ptr = "Stackoverflow" ; //correct

/*taking a int pointer and assigning a string to it*/
int *p = 22 ; //incorrect

printf("%s",ptr); // correct and print
printf("%d",p); //incorrect and give compile time error.

return 0;
}
Run Code Online (Sandbox Code Playgroud)

c literals string-literals

5
推荐指数
1
解决办法
1270
查看次数

标签 统计

c ×1

literals ×1

string-literals ×1