分配中不兼容的类型?

时间:2010-09-30 15:01:11

标签: c

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

#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");
     }

3 个答案:

答案 0 :(得分:4)

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

char *myPointer[20];

但是你要为它分配一个字符数组:

myPointer = myFiend[0];

这与以下内容相同:

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

可能你的意思是要使myPointer只是一个指向字符串的指针:

char *myPointer;
myPointer = myFriend[0];

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

myPointer[0] = myFriend[0];

答案 1 :(得分:1)

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

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

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

请参阅左右步行方法:www.cs.uml.edu/~canning/101/RLWM.pdf

答案 2 :(得分:0)

char* myPointer[20];
char myFriend[][150];
/* ... */
myPointer = myFriend[0]; /* error! */

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

,则赋值将有效