为什么char*直接设置该值,而在c编程中没有int*的情况下
在C中没有字符串作为原始数据类型的存在 语言。 char*使字符串存在。如果字符设置为 像这样的指针(char *msg =“ hi!”)但是为什么不在 int类型的指针像这样(int *p = 10)?
这是我的代码:
#include <stdio.h>
int main(void)
{
char *msg = "HI!";
int *p = 2;
printf("%p\n", msg);
printf("%p\n", &msg[0]);
printf("%p\n", p);
}
当我尝试编译时,我收到了一条错误消息。下面的错误消息:
pointers.c:6:10: error: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int' [-Werror,-Wint-conversion]
int *p = 2;
^ ~
1 error generated.
make: *** [<builtin>: pointers] Error 1
There is no existence of string as a primitive data type in c
language. char* make string exist. If a list of character set to an
pointer like this (char *msg = "HI!") but why not in the case of
pointer of type int like this (int *p = 10)?
Here is my code:
#include <stdio.h>
int main(void)
{
char *msg = "HI!";
int *p = 2;
printf("%p\n", msg);
printf("%p\n", &msg[0]);
printf("%p\n", p);
}
I got an error message when I tried to compile it. Error message down below:
pointers.c:6:10: error: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int' [-Werror,-Wint-conversion]
int *p = 2;
^ ~
1 error generated.
make: *** [<builtin>: pointers] Error 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您说
几个特殊的事情正在发生时。首先,字符串文字
“ hi!”
被视为匿名数组(这种对字符串文字的处理是C语言 dos 支持的唯一方法 “字符串类型”的概念。)
第二,当您尝试使用此数组的值时,您得到的(就像在C中一样)是指向数组的第一个元素的指针:
而且效果很好。另一方面,当您
没有这样说时。如果有的话,这告诉编译器不是您想要一个指向整数2的指针> p ,而是您想要一个指针
p
指向内存地址2 。但是您的程序可能无法访问内存地址2,即使这样做,对于普通程序,也无法知道该地址的内容,您无能为力。 (另一方面,如果您进行嵌入式编程,您可能会知道,该地址在特定体系结构上有一些有趣的东西。)如果您想要
p
指向带有值的整数对象2,您需要以某种方式创建该整数对象。您可以做或
或 - 这最终更接近字符串初始化的工作方式 - 您可以使用一种特殊的语法,称为a compound compound literal :
When you say
several special things are happening. First, the string literal
"HI!"
is treated like an anonymous array(This treatment of string literals is one and perhaps the only way in which the C language does support the notion of a "string type".)
Second, when you attempt to use the value of this array, what you get (as you just about always do in C) is a pointer to the array's first element:
And that works well. On the other hand, when you say
nothing like that happens. If anything, this tells the compiler not that you want a pointer
p
pointing at the integer 2, but rather, that you want a pointerp
that points at memory address 2. But your program probably doesn't have access to memory address 2, and even if it did, for a normal program, there's no way to know what's at that address, and nothing meaningful you could do with it. (If you're doing embedded programming, on the other hand, you might know that there's something interesting at that address for your particular architecture.)If you want
p
to point to an integer object with the value 2, you'll need to create that integer object somehow. You could door
Or — and this ends up being much closer to the way string initialization works — you can use a special syntax called a compound literal:
字符串文字具有字符阵列的类型。例如,字符串文字
“ hi!”
具有类型char [4]
。 使用此简单程序检查一下您可以从C标准(6.4.5字符串文字)中
在表达式数组指定器中使用的,极少数例外的人会隐式转换为指针转换为其第一元素,例如在此声明中
等同于
C标准(6.3.2.1 lvalues,arrays and arrays and function指定者)
整数数组一样
因此,如果您像可以写的
String literals have types of character arrays. For example the string literal
"HI!"
has typechar[4]
. You can check that with this simple programFrom the C Standard (6.4.5 String literals)
Used in expressions array designators with rare exceptions are implicitly converted to pointers to their first elements like in this declaration
that is equivalent to
From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)
So if you had an integer array like
you could write