2010-09-30 5 views
0

Что не так с той строкой, которую я указал ниже?Несовместимые типы в задании?

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

Формат вашего кода !!! –

+0

'scanf' и'% s' без спецификатора ширины = «привет, переполнение буфера». –

ответ

1

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

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

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

Обратитесь к методу вправо-влево ходьбы: www.cs.uml.edu/~canning/101/RLWM.pdf

4

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

char *myPointer[20]; 

Но вы присваиваете ему массив символов:

myPointer = myFiend[0]; 

Который equivilent к:

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

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

char *myPointer; 
myPointer = myFriend[0]; 

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

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

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

Смежные вопросы