Несовместимые типы в назначении? - PullRequest
0 голосов
/ 30 сентября 2010

Что не так с той строкой, которую я указал ниже?

#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 ]

4 голосов
/ 30 сентября 2010

Вы определили myPointer как массив символьных указателей:

char *myPointer[20];

Но вы назначаете ему массив символов:

myPointer = myFiend[0];

Что эквивалентно:

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

Возможно, вы имеете в виду либо сделать myPointer просто указателем на строку:

char *myPointer;
myPointer = myFriend[0];

или вы хотите сделать первую строку, на которую указывает myPointer строка myFriend[0]:

myPointer[0] = myFriend[0];
1 голос
/ 30 сентября 2010

myPointer - это массив (20) указателей на символы.myFriend [0] представляет собой массив (150) символов.Типы не совпадают.

По сути, myFriend [0] является адресом первого символа последовательности символов.Это адрес буквы «Т» в «Автомобиль был наконец обнаружен ....».

myPointer - это массив из 20 указателей на символы.Увидеть разницу?Кроме того, вы не можете выполнять присваивание, даже если типы совпадают, потому что myPointer (адрес массива из 20 указателей на символы) является постоянным значением.Я полагаю, что стандарт C определенно утверждает, что это неопределенное поведение.

См. Метод обхода справа налево: www.cs.uml.edu / ~ canning / 101 / RLWM.pdf

0 голосов
/ 30 сентября 2010
char* myPointer[20];
char myFriend[][150];
/* ... */
myPointer = myFriend[0]; /* error! */

myFriend[0] - это массив из 150 символов.Его можно автоматически преобразовать в указатель char*, указывающий на первый элемент.Но myPointer - это массив указателей.Вы не можете ничего присвоить этому.Назначение будет действительным, если тип myPointer был (например) char* myPointer;

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...