2010-09-30 5 views
0

Quel est le problème avec cette ligne que j'ai indiquée ci-dessous?Types incompatibles dans l'affectation?

#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

Formatez votre code !!! –

+0

'scanf' et'% s' sans spécificateur de largeur = "bonjour, buffer overflow." –

Répondre

1

myPointer est un tableau de (20) pointeurs vers des caractères. myFriend [0] est un tableau de (150) caractères. Les types ne correspondent pas.

Essentiellement, myFriend [0] est l'adresse du premier caractère d'une séquence de caractères. C'est l'adresse de la lettre "T" dans "La voiture a finalement été découverte ....". MyPointer est un tableau de 20 pointeurs vers des caractères.

Regarde la différence? En outre, vous ne pouvez pas effectuer l'affectation même si les types correspondent, car myPointer (l'adresse du tableau de 20 pointeurs vers des caractères) est une valeur constante. Je crois que la norme C stipule spécifiquement qu'il s'agit d'un comportement indéfini.

Reportez-vous à la méthode de marche droite-gauche: www.cs.uml.edu/~canning/101/RLWM.pdf

4

Vous avez défini myPointer être un tableau de pointeurs de caractères:

char *myPointer[20]; 

Mais vous lui assigner un tableau de caractères:

myPointer = myFiend[0]; 

Ce qui est equivilent à:

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

Peut-être vous dire soit de faire myPointer simplement un pointeur sur une chaîne:

char *myPointer; 
myPointer = myFriend[0]; 

ou signifie pour vous faire la première chaîne pointée par myPointer la chaîne myFriend[0]:

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

myFriend[0] est un tableau de 150 caractères. Il peut être automatiquement converti en un pointeur char* pointant sur le premier élément. Mais myPointer est un tableau de pointeurs. Vous ne pouvez rien attribuer à cela. L'affectation serait valide si le type de myPointer était (par exemple) char* myPointer;

Questions connexes