C语言 警告:赋值从指针生成整数而不进行强制转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5122231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:53:08  来源:igfitidea点击:

warning: assignment makes integer from pointer without a cast

ccasting

提问by lcutler

When I declare a char *to a fixed string and reuse the pointer to point to another string

当我声明一个char *固定字符串并重用指针指向另一个字符串时

/* initial declaration */
char *src = "abcdefghijklmnop";
.....

/* I get the   "warning: assignment makes integer from pointer without a cast" */
*src ="anotherstring";

I tried to recast the pointer but no success.

我试图重铸指针但没有成功。

回答by Jeremiah Willcock

The expression *srcrefers to the first character in the string, not the whole string. To reassign srcto point to a different string tgt, use src = tgt;.

表达式*src指的是字符串中的第一个字符,而不是整个字符串。要重新分配src指向不同的字符串tgt,请使用src = tgt;.

回答by bta

When you write the statement

当你写声明时

*src = "anotherstring";

the compiler sees the constant string "abcdefghijklmnop"like an array. Imagine you had written the following code instead:

编译器将常量字符串视为"abcdefghijklmnop"数组。想象一下,您已经编写了以下代码:

char otherstring[14] = "anotherstring";
...
*src = otherstring;

Now, it's a bit clearer what is going on. The left-hand side, *src, refers to a char(since srcis of type pointer-to-char) whereas the right-hand side, otherstring, refers to a pointer.

现在,发生了什么事情更清楚了。左侧,*src,指的是一个char(因为src是指针到- 类型char),而右侧,otherstring,指的是一个指针。

This isn't strictly forbidden because you may want to store the address that a pointer points to. However, an explicit cast is normally used in that case (which isn't too common of a case). The compiler is throwing up a red flag because your code is likely not doing what you think it is.

这并不是严格禁止的,因为您可能想要存储指针指向的地址。但是,在这种情况下通常使用显式强制转换(这种情况不太常见)。编译器抛出了一个危险信号,因为您的代码可能没有按照您的想法行事。

It appears to me that you are trying to assign a string. Strings in C aren't data types like they are in C++ and are instead implemented with chararrays. You can't directly assign values to a string like you are trying to do. Instead, you need to use functions like strncpyand friends from <string.h>and use chararrays instead of charpointers. If you merely want the pointer to point to a different static string, then drop the *.

在我看来,您正在尝试分配一个字符串。C 中的字符串不是 C++ 中的数据类型,而是用char数组实现的。您不能像尝试那样直接为字符串赋值。相反,您需要使用类似strncpy和朋友来自的函数,<string.h>并使用char数组而不是char指针。如果您只想让指针指向不同的静态字符串,请删除*.

回答by John Bode

The warning comes from the fact that you're dereferencing srcin the assignment. The expression *srchas type char, which is an integral type. The expression "anotherstring"has type char [14], which in this particular context is implicitly converted to type char *, and its value is the address of the first character in the array. So, you wind up trying to assign a pointer value to an integral type, hence the warning. Drop the *from *src, and it should work as expected:

警告来自您src在作业中取消引用的事实。该表达式*src具有 type char,它是一个整数类型。表达式"anotherstring"有 type char [14],在这个特定的上下文中它被隐式转换为 type char *,它的值是数组中第一个字符的地址。因此,您最终尝试将指针值分配给整数类型,因此出现警告。删除*from *src,它应该按预期工作:

src = "anotherstring";

since the type of srcis char *.

因为类型srcchar *.

回答by Pete Wilson

What Jeremiah said, plus the compiler issues the warning because the production:

耶利米所说的,加上编译器发出警告,因为生产:

*src ="anotherstring";

says: take the address of "anotherstring" -- "anotherstring" IS a char pointer -- and store that pointer indirect through src (*src = ... ) into the first char of the string "abcdef..." The warning might be baffling because there is nowhere in your code any mention of any integer: the warning seems nonsensical. But, out of sight behind the curtain, is the rule that "int" and "char" are synonymous in terms of storage: both occupy the same number of bits. The compiler doesn't differentiate when it issues the warning that you are storing into an integer. Which, BTW, is perfectly OK and legal but probably not exactly what you want in this code.

说:取“anotherstring”的地址——“anotherstring”是一个字符指针——并通过 src (*src = ... ) 将该指针间接存储到字符串“abcdef ...”的第一个字符中 警告可能令人困惑,因为在您的代码中没有任何地方提到任何整数:警告似乎毫无意义。但是,幕后看不到的是“int”和“char”在存储方面是同义词的规则:两者都占用相同的位数。编译器在发出您存储为整数的警告时不会区分。顺便说一句,这完全可以且合法,但可能不完全是您在此代码中想要的。

-- pete

——皮特