make integer from pointer without a cast

huangapple go评论62阅读模式
英文:

make integer from pointer without a cast

问题

" makes integer from pointer without a cast" means that there is an attempt to assign a pointer (in this case, a character pointer) to an integer variable (in this case, a char variable) without explicitly converting the pointer to an integer type. This can lead to issues because a pointer is not the same as an integer, and such assignments may cause unexpected behavior or errors in the code.

英文:
assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
   31 |         array[kategori] = "Best Pick";
      |                         ^
t02.c:36:25: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
   36 |         array[kategori] = "Must Read";
      |                         ^
t02.c:40:25: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
   40 |         array[kategori] = "Recommended";
      |                         ^
t02.c:44:25: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
   44 |         array[kategori] = "Average";
      |                         ^
t02.c:47:25: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
   47 |         array[kategori] = "Low";

i want to know, what " makes integer from pointer without a cast" does mean ?

答案1

得分: 2

这意味着你试图将一个整数设置为一个指针,就像这样:

const char * ptrToChar = "hello";
int x = ptrToChar;   // 错误,试图将整数设置为指针的值

同样,在这种情况下,你试图设置的整数类型是 char(也称为一个 7 位或 8 位整数,通常用于存储一个 ASCII 字符):

const char * ptrToChar = "hello";
char x = ptrToChar;  // 错误,试图将 char 设置为 (const char *)

从你得到的警告看,你试图设置的 char 是一个 char 数组的成员,所以可能更像是这样:

int kategori = 3;
char array[20];   // (array) 中的每个元素都是 char
array[kategori] = "Recommended"; // 错误,试图将 char 设置为 (const char *)

由于 array 中的每个元素都是 char,你只能真正地在其中存储一个 char,像这样:

array[kategori] = 'R';  // 可行(请注意 'R' 周围的单引号)

如果你想将整个字符串复制到 array 中,你可以逐个字符地执行,使用 for 循环,或者使用类似 strncpy() 这样的 libC 提供的函数来完成:

strncpy(array, "Recommended", sizeof(array));

将整数设置为指针的值在技术上是可能的(在某种程度上),但这几乎永远没有意义,这就是为什么编译器警告你尝试让它执行你可能并不真正打算做的事情的原因。

英文:

It means you're trying to set an integer equal to a pointer, like this:

const char * ptrToChar = "hello";
int x = ptrToChar;   // error, trying to set an integer equal to the value of a pointer

Also in this case the type of the integer you're trying to set is char (aka a 7-bit or 8-bit integer that is commonly used to hold exactly 1 ASCII character):

const char * ptrToChar = "hello";
char x = ptrToChar;  // error, trying to set a char equal to a (const char *)

From the looks of the warnings you are getting, the char you're trying to set is a member of a char-array, so probably it's more like this:

int kategori = 3;
char array[20];   // each element of (array) is a char
array[kategori] = "Recommended"; // error, trying to set a char equal to a (const char *)

Since each element in array is a char, you can only really store a char in it, like this:

array[kategori] = 'R';  // ok (note single quotes around 'R')

... if you wanted to copy an entire string into array, you could do it character-by-character, using a for-loop, or use a libC-supplied function like strncpy() to do it for you:

strncpy(array, "Recommended", sizeof(array));

Setting an integer to the value of a pointer is technically possible (sort of), but it almost never makes sense to do that, which is why the compiler is warning you that you are trying to have it do something that you probably didn't really intend.

huangapple
  • 本文由 发表于 2023年2月8日 12:37:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定