【问题标题】:Incompatible Types in Assignment?分配中的不兼容类型?
【发布时间】:2010-09-30 23:14:11
【问题描述】:

我在下面指出的那条线有什么问题?

#include <stdio.h>
#include<time.h>
main ()
{
     int myRandom;
     char* myPointer[20];
     char yourName[20];
     char yourAge[20];
     char myPal[20];
     char yourDOB[20];
     char yourCartype[20];
     char yourFavoritecolor[20];
     char myFriend [][150] = {"The car was finally discovered in Dr. Yamposlkiy's parking spot at UofL.",
                             "The Car was never found.",
                             "The Police were unable to find the car so out of the kindness of their heart they gave him a new one.",
                             "The car was finally discovered in Cabo San Lucas where it was sold for drug money.",
                             "The car was finally discovered in his driveway, how it got there, we will never know.",
                             "The car was finally found in a junk yard where they were unable to piece it back together.",
                             "The car was found but greatly vandalized so he just decided it was best to get that Lexus he really wanted.",
                             "The car wasn't found so he went to his insurance company where they just blew him off to do something on his own."};
     myPointer = myFriend[0];  // Error is on this line
     srand(time(0));
     myRandom = rand() % 8;
     printf("Your name here: ");
     scanf("%s", yourName);
     printf("Your Best Friends name here: ");
     scanf("%s", myPal);
     printf("Your age here: ");
     scanf("%s", yourAge);
     printf("Your DOB here (ex 1/1/1901): ");
     scanf("%s", yourDOB);
     printf("Your Car Type here (ex Carolla): ");
     scanf("%s", yourCartype);
     printf("Your Favoite Color here: ");
     scanf("%s", yourFavoritecolor);
     printf("\n\nYour Name is %s!\n", yourName);
     printf("\n\nYour Age is %s!\n", yourAge);
     printf("\n\nYour DOB is %s!\n", yourDOB);
     printf("\n\nYour Car Type is %s!\n", yourCartype);
     printf("\n\nYour Favorite Color is %s!\n", yourFavoritecolor);
     printf("\n\nWith the given information here is a nice story");
     printf("\n\n\n %s and his friend %s were driving his car, a %s %s.\n", yourName, myPal, yourFavoritecolor, yourCartype);
     printf("%s and %s decided that they were going get something to eat at McDonalds", yourName, myPal);
     printf("where %s 's %s %s was stolen.\n", yourName, yourFavoritecolor, yourCartype);
     printf("They decided it would be best to go to the Police Station to let someone know.\n");
     printf("While they were there, they asked for %s 's Date of Birth, which is %s,\n", yourName, yourDOB);
     printf("which makes him %s years old.\n", yourAge);
     printf("%s", myFriend);
     printf("\n\n\n\n THE END \n HOPE YOU ENJOYED IT \n\n");
     system("pause");
     }

【问题讨论】:

  • scanf%s 没有任何宽度说明符 = "你好,缓冲区溢出。"

标签: c


【解决方案1】:

您已将 myPointer 定义为字符指针数组:

char *myPointer[20];

但是你给它分配了一个字符数组:

myPointer = myFiend[0];

相当于:

myPointer = "... stuff ...";

您的意思可能是让myPointer 只是一个指向字符串的指针:

char *myPointer;
myPointer = myFriend[0];

或者你的意思是把myPointer指向的第一个字符串变成myFriend[0]字符串:

myPointer[0] = myFriend[0];

【讨论】:

    【解决方案2】:

    myPointer 是一个由 (20) 个指向字符的指针组成的数组。 myFriend[0] 是 (150) 个字符的数组。类型不匹配。

    本质上,myFriend[0] 是字符序列的第一个字符的地址。这是“汽车终于被发现……”中字母“T”的地址。

    myPointer 是一个包含 20 个字符指针的数组。看到不同?此外,即使类型匹配,也无法执行赋值,因为 myPointer(20 个字符指针数组的地址)是一个常量值。我相信 C 标准明确指出这是未定义的行为。

    参考左右走法:www.cs.uml.edu/~canning/101/RLWM.pdf

    【讨论】:

      【解决方案3】:
      char* myPointer[20];
      char myFriend[][150];
      /* ... */
      myPointer = myFriend[0]; /* error! */
      

      myFriend[0] 是一个 150 个字符的数组。它可以自动转换为指向第一个元素的char* 指针。但是myPointer 是一个指针数组。你不能给它分配任何东西。如果 myPointer 的类型是(例如)char* myPointer;

      ,则分配将是有效的

      【讨论】:

        猜你喜欢
        • 2012-06-26
        • 1970-01-01
        • 2011-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-10
        • 2013-09-15
        相关资源
        最近更新 更多