在 C 中,为什么不能像将字符串值分配给 char* 一样将整数值分配给 int*?

编程入门 行业动态 更新时间:2024-10-19 17:18:59
本文介绍了在 C 中,为什么不能像将字符串值分配给 char* 一样将整数值分配给 int*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在浏览网站,但还没有找到这个问题的答案.

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 的指针到字符类型的变量,其值是字符串文字hello"的第一个字符('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 的指针到 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";

将内容为 "hello" 的数组的初始元素的地址分配给指针 mystr(后跟终止 '' 空字符).顺便说一句,这样写更安全:

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} 是一个复合字面量,它创建了一个初始化为 42 的匿名 int 对象;& 获取该对象的地址.

(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 中,为什么不能像将字符串值分配给 char* 一样将整数值分配给 int*?

本文发布于:2023-07-29 08:53:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   能像   字符串值   char   int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!