在 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*?

查看:18
本文介绍了在 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;
}

我对第一个程序的理解是,它创建了一个类型为pointer-to-char的名为mystr的变量,其值是字符串文字hello"的第一个char('h')的地址.换句话说,通过这种初始化,您不仅可以获取指针,还可以定义指针指向的对象(在本例中为hello").

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 *myint = 5; 似乎没有实现与此类似的东西,即创建一个名为 myint 的类型为pointer-to-int的变量,其值是地址值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.与任何数组表达式一样,它是隐式转换的,在大多数但不是所有情况下,指向指向数组第一个元素的指针.所以这个:

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 分配一个数组的初始元素的地址,该数组的内容是 "hello"(后跟终止 '' 空字符).顺便说一句,这样写更安全:

assigns to the pointer mystr the address of the initial element of an array whose contents are "hello" (followed by a terminating '' 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* 类型的对象.(严格来说是初始化而不是赋值,但效果是一样的).由于没有从 intint* 的隐式转换(除了将 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天全站免登陆