在C语言中,为什么不能整数值被分配到一个int *以同样的方式的字符串值可以被分配到一个char *? [英] In C, why can't an integer value be assigned to an int* the same way a string value can be assigned to a char*?

查看:207
本文介绍了在C语言中,为什么不能整数值被分配到一个int *以同样的方式的字符串值可以被分配到一个char *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直期待通过该网站,但还没有找到答案这一个呢。

I've been looking through the site but haven't found an answer to this one yet.

这是最简单的(至少对我来说)来解释用一个例子这个问题。

It is easiest (for me at least) to explain this question with an example.

我不明白为什么这是有效的:

I don't understand why this is valid:

#include <stdio.h>

int main(int argc, char* argv[])
{
  char *mystr = "hello";
}

但是,这会产生一个编译器警告(初始化将指针从整数,未作类型转换):

But this produces a compiler warning ("initialization makes pointer from integer without a cast"):

#include <stdio.h>

int main(int argc, char* argv[])
{
  int *myint = 5;
}

我的第一个节目的理解是,创建一个名为类型的指针到焦炭myStr的变量,它的值是字符串你好的第一个字符(H)的地址。在与此初始化换句话说你不仅获得指针,但也可以定义(在这种情况下,你好)的对象,该对象的指针指向

My understanding of the first program is that creates a variable called mystr of type pointer-to-char, the value of which is the address of the first char ('h') of the string literal "hello". In other words with this initialization you not only get the pointer, but also define the object ("hello" in this case) which the pointer points to.

那么,为什么做为int *敏= 5; 貌似没有实现的东西类似于此,即创建一个名为类型的指针到整型敏变量时,它的值是值的地址'5'?为什么没有这个初始化既给我指针,还定义了对象,指针指向?

Why, then, does int *myint = 5; seemingly not achieve something analogous to this, i.e. create a variable called myint of type pointer-to-int, the value of which is the address of the value '5'? Why doesn't this initialization both give me the pointer and also define the object which the pointer points to?

推荐答案

在事实上,你可以这样做,用的复合文字的,这一特点在1999 ISO C标准添加到语言。

In fact, you can do so using a compound literal, a feature added to the language by the 1999 ISO C standard.

字符串文字的类型为的char [N] ,其中 N 是字符串加1的长度。像任何阵列前pression,它隐含的转换的,在大多数但不是所有的上下文,一个指向数组的第一个元素。因此,这:

A string literal is of type char[N], where N is the length of the string plus 1. Like any array expression, it's implicitly converted, in most but not all contexts, to a pointer to the array's first element. So this:

char *mystr = "hello";

分配给指针 myStr中的数组,其内容是初始元素的地址你好(随后终止'\\ 0'空字符)。
顺便说一句,它的安全写:

assigns to the pointer mystr the address of the initial element of an array whose contents are "hello" (followed by a terminating '\0' null character). Incidentally, it's safer to write:

const char *mystr = "hello";

有整数没有这样的隐式转换 - 但你可以这样做:

There are no such implicit conversions for integers -- but you can do this:

int *ptr = &(int){42};

(INT){42} 是一个复合文字,它创建一个匿名 INT 对象初始化为 42 ; &放大器; 需要的对象的地址

(int){42} is a compound literal, which creates an anonymous int object initialized to 42; & takes the address of that object.

但要注意:由一个字符串文字始终创建该阵列具有静态存储时间,而是由复合文字创建的对象可以是静态的或自动存储时间,取决于它出现的地方。这意味着,如果 PTR 从函数返回,用该值的对象 42 将不复存在值而指针仍然指向它。

But be careful: The array created by a string literal always has static storage duration, but the object created by a compound literal can have either static or automatic storage duration, depending on where it appears. That means that if the value of ptr is returned from a function, the object with the value 42 will cease to exist while the pointer still points to it.

至于

int *myint = 5;

这尝试分配值 5 来类型的对象为int * 。 (严格地说它是一个初始化,而不是分配,但效果是一样的)。由于有来自 INT 为int * (除特殊情况 0的隐式转换被视为一个空指针常数),这是无效的。

that attempts to assign the value 5 to an object of type int*. (Strictly speaking it's an initialization rather than an assignment, but the effect is the same). Since there's no implicit conversion from int to int* (other than the special case of 0 being treated as a null pointer constant), this is invalid.

这篇关于在C语言中,为什么不能整数值被分配到一个int *以同样的方式的字符串值可以被分配到一个char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆