【问题标题】:initialization makes integer from pointer without a cast [enabled by default]初始化从没有强制转换的指针生成整数 [默认启用]
【发布时间】:2014-06-21 01:01:13
【问题描述】:

我刚开始学习如何编程,我遇到了这样的错误:“初始化从没有强制转换的指针生成整数 [默认启用]” 有什么问题?

 // This program pairs three kids with their favorite superhero
#include <stdio.h>
#include <string.h>
main()
{
char Kid1[12];
// Kid1 can hold an 11-character name
// Kid2 will be 7 characters (Maddie plus null 0)
char Kid2[] = "Maddie";
// Kid3 is also 7 characters, but specifically defined
char Kid3[7] = "Andrew";
// Hero1 will be 7 characters (adding null 0!)
char Hero1 = "Batman";
// Hero2 will have extra room just in case
char Hero2[34] = "Spiderman";
char Hero3[25];
Kid1[0] = 'K';  //Kid1 is being defined character-by-character
Kid1[1] = 'a';  //Not efficient, but it does work
Kid1[2] = 't';
Kid1[3] = 'i';
Kid1[4] = 'e';
Kid1[5] = '\0';  // Never forget the null 0 so C knows when the
// string ends
strcpy(Hero3, "The Incredible Hulk");


printf("%s\'s favorite hero is %s.\n", Kid1, Hero1);
printf("%s\'s favorite hero is %s.\n", Kid2, Hero2);
printf("%s\'s favorite hero is %s.\n", Kid3, Hero3);
return 0;
}

【问题讨论】:

  • 无论你做什么不要添加演员!!错误信息可以改进很多:)
  • //Not efficient, but it does work - 这表明你已经在担心效率了。 不要! 至少当你是一个绝对的初学者时不会。

标签: c


【解决方案1】:

问题出在char Hero1 = "Batman":

  • 当您在代码中使用双引号字符串时,编译器会将其替换为指向该字符串在运行时将驻留的内存空间开头的指针。
  • 所以char Hero1 = "Batman",您实际上是在尝试将内存地址(通常由 32 或 64 位数据组成,具体取决于您的系统)分配给字符变量(通常存储 8 位数据)。

为了解决问题,您需要将其更改为以下选项之一:

  • char Hero1[] = "Batman"
  • char* Hero1 = "Batman"

仅供参考,在上述两种情况下,字符串 "Batman" 在运行时将驻留在只读内存部分中。

但是,这两种情况之间存在显着差异:

  • 使用char Hero1[],每次调用函数时都会将"Batman" 字符串复制到堆栈中。 Hero1 数组将从该地址开始,您可以稍后在函数内更改该数组的内容。
  • 使用char* Hero1,每次调用函数时,"Batman" 字符串不会复制到堆栈中。 Hero1 变量将指向字符串的原始地址,因此您将能够在函数内的任何位置更改该字符串的内容。

当从您的代码生成可执行映像时,字符串被放置在代码段中,代码段是程序中的几个内存段之一。然后编译器将所谓的“字符串赋值”替换为所谓的“整数赋值”。

例如,char* x = "abc" 在编译成目标代码之前被更改为char* x = (char*)0x82004000,其中0x82004000 是字符串在程序内存空间中的(常量)地址。

当您执行sizeof("abc") 时,可执行映像甚至不会包含"abc" 字符串,因为没有对此字符串执行“运行时”操作。

没有为sizeof 生成目标代码 - 编译器在编译期间计算此值,并立即将其替换为常量

您可以查看通常生成的(中间)映射文件,并看到sizeof 操作的输入字符串没有出现在任何地方。

【讨论】:

    【解决方案2】:
    char Hero1 = "Batman";
    

    应该是

    char Hero1[] = "Batman";
    

    【讨论】:

      【解决方案3】:

      有几次你有一些问题:

      #include <stdio.h>
      #include <string.h>
      main()
      {
          char Kid1[12];
          // Kid1 can hold an 11-character name
          // Kid2 will be 7 characters (Maddie plus null 0)
          char Kid2[] = "Maddie";
          // Kid3 is also 7 characters, but specifically defined
          char Kid3[7] = "Andrew";
          // Hero1 will be 7 characters (adding null 0!)
          char *Hero1 = "Batman"; //needs to be a pointer
          // Hero2 will have extra room just in case
          char *Hero2 = "Spiderman"; //needs to be a pointer
          char Hero3[25]
          Kid1[0] = 'K';  //Kid1 is being defined character-by-character
          Kid1[1] = 'a';  //Not efficient, but it does work
          Kid1[2] = 't';
          Kid1[3] = 'i';
          Kid1[4] = 'e';
          Kid1[5] = '\0';  // Never forget the null 0 so C knows when the
                        // string ends
          strcpy(Hero3, "The Incredible Hulk");
      
      
          printf("%s\'s favorite hero is %s.\n", Kid1, Hero1);
          printf("%s\'s favorite hero is %s.\n", Kid2, Hero2);
          printf("%s\'s favorite hero is %s.\n", Kid3, Hero3);
          return 0;
      }
      

      您应该在函数顶部定义所有变量,这是一个很好的 C 实践。

      除此之外,我用 cmets 标记了这些问题(并纠正了它们)。

      【讨论】:

        【解决方案4】:

        解决方案:

        您收到此错误是因为 字符串数据类型不在 C 语言中 编程您可以使用数组或字符指针来打印字符串,例如

        1.数组

          #include<stdio.h>
           int main(){
           char  a[]={'a','b','c','d','f','\0'};
           printf("%s",a);
           return 0;
           }
        

        Click here to check the output of solution array

        2.字符指针

            #include<stdio.h>
             int main(){
             char*  a="abcd";
             printf("%s",a);
             return 0;
             }
        

        Click here to check the output of solution char pointer

        【讨论】:

        • 第一个版本不正确。 C 不会自动对数组进行空终止。你需要做char a[] = {'a', 'b', 'c', 'd', 'f', '\0'};
        • 另外,请努力修正答案的缩进和格式。
        • 先生的第一个程序显示了为什么您的程序中发生了该错误,其余两个是解决它的解决方案
        • 我不是 OP(顺便说一句,这个问题是 6 年前发布的),我正在评论这篇文章,因为它被标记为低质量的答案,建议你 fix up the formatting in the answer。我指的bug是针对你提出的使用数组的解决方案,C无效,请重新检查。
        • 先生,所有解决方案都处于工作状态,我还附上了我的代码和输出的屏幕截图,我检查了 programiz C 编译器上的代码并更改了我的答案格式
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多