Incompatible Types in Assignment?
What is wrong with that line I have indicated below?
#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");
}
You've defined myPointer
to be an array of character pointers:
char *myPointer[20];
But you're assigning to it an array of characters:
myPointer = myFiend[0];
Which is equivilent to:
myPointer = "... stuff ...";
Possibly you mean either to make myPointer
simply a pointer to a string:
char *myPointer;
myPointer = myFriend[0];
or you mean to make the first string pointed to by myPointer
the myFriend[0]
string:
myPointer[0] = myFriend[0];
myPointer is an array of (20) pointers to characters. myFriend[0] is an array of (150) characters. The types do not match.
Essentially, myFriend[0] is the address of the first character of a sequence of characters. This is the address of the letter 'T' in "The car was finally discovered....".
myPointer is an array of 20 pointers to characters. See the difference? Also, you cannot perform the assignment even if types matched, because myPointer (the address of the array of 20 pointers to characters) is a constant value. I believe the C standard specifically states this is undefined behavior.
Refer to the right-left walk method: www.cs.uml.edu/~canning/101/RLWM.pdf
char* myPointer[20];
char myFriend[][150];
/* ... */
myPointer = myFriend[0]; /* error! */
myFriend[0]
is an array of 150 chars. It can be automatically converted to a char*
pointer pointing at the first element. But myPointer
is an array of pointers. You can't assign anything to that. The assignment would be valid if the type of myPointer were (for example) char* myPointer;
精彩评论