Thread: warning: assignment from incompatible pointer type.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    warning: assignment from incompatible pointer type.

    hey everyone i get that error message for the line at the bottom that says "nameofteama = &team1[0].name;" please is there anyone who knows why im getting the error message in wrote in the title. i dont want the program messed up now. BY THE WAY don't comment if it's going to be "oh you suck at programmin" and stuff like that i just need help. thanks
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAXTEAMS 16
    typedef struct team
    {
        char name[50];
        int points;
        int scored;
    }teams;
    
    
    
    
    teams team1[MAXTEAMS] = {{"a"},{"b"},{"c"},{"d"},{"e"},{"f"},{"g"},{"h"},{"i"},{"j"},{"k"},{"l"},{"m"},{"n"},{"o"},{"p"}};
    
    
    int main()
     {
         team1[0].scored = 0;
         team1[1].scored = 0;
         team1[2].scored = 0;
         team1[3].scored = 0;
         team1[4].scored = 0;
         team1[5].scored = 0;
         team1[6].scored = 0;
         team1[7].scored = 0;
         team1[8].scored = 0;
         team1[9].scored = 0;
         team1[10].scored = 0;
         team1[11].scored = 0;
         team1[12].scored = 0;
         team1[13].scored = 0;
         team1[14].scored = 0;
         team1[15].scored = 0;
        int i;
    
    
                for(i=0;i<MAXTEAMS;i++)
                {
                    printf("enter team:\n");
                    scanf("%s",team1[i].name);
                }
                printf("welcome to the champions league\n\n");
    
    
    
    
                         for(i=1;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[0].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[0].name);
                            scanf("%d",&team1[0].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[0].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                                 for(i=2;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[1].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[1].name);
                            scanf("%d",&team1[1].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[1].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=3;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[2].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[2].name);
                            scanf("%d",&team1[2].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[2].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=4;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[3].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[3].name);
                            scanf("%d",&team1[3].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[3].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=5;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[4].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[4].name);
                            scanf("%d",&team1[4].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[4].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=6;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[5].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[5].name);
                            scanf("%d",&team1[5].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[5].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=7;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[6].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[6].name);
                            scanf("%d",&team1[6].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[6].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=8;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[7].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[7].name);
                            scanf("%d",&team1[7].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[7].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=9;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[8].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[8].name);
                            scanf("%d",&team1[8].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[8].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=10;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[9].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[9].name);
                            scanf("%d",&team1[9].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[9].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=11;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[10].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[10].name);
                            scanf("%d",&team1[10].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[10].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=12;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[11].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[11].name);
                            scanf("%d",&team1[11].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[11].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=13;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[12].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[12].name);
                            scanf("%d",&team1[12].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[12].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=14;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[13].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[13].name);
                            scanf("%d",&team1[13].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[13].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                            printf("%s vs %s\n",team1[14].name,team1[15].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[14].name);
                            scanf("%d",&team1[14].scored);
                            printf("%s's score:",team1[15].name);
                            scanf("%d",&team1[15].scored);
                            team1[14].scored += team1[0].scored;
                            team1[15].scored += team1[i].scored;
    char *nameofteama;
    int *scoreofteama;
    for(i=1;i<MAXTEAMS;i++)
    {
        if(team1[0].scored > team1[i].scored)
        {
            nameofteama = &team1[0].name;
            scoreofteama = &team1[0].scored;
        }
        printf("best team is %s with %d points",*nameofteama,*scoreofteama);
    }
    
    
    return(0);
    }
    Last edited by ingeniousreader; 03-06-2012 at 08:39 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *nameofteama;
    int *scoreofteama;
    for(i=1;i<MAXTEAMS;i++)
    {
        if(team1[0].scored > team1[i].scored)
        {
            nameofteama = &team1[0].name;
            scoreofteama = &team1[0].scored;
        }
        printf("best team is %s with %d points",*nameofteama,*scoreofteama);
    }
    I'm not sure why you are hard coding 0 in there. All you really need is an int to keep track of what slot in the list has the best score, then simply do:
    Code:
    if this is greater than that
        bestscore = this
    printf( "best team is %s with %d points\n", teams[ bestscore ].name, teams[ bestscore ].score ] );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    i see what you are getting at but i want to get everyteam in order of who has the highest score down to who has the lowest score. the user inputs many scores because the teams play each other many times so i dont know the scores in advance . so what i want to do is a for loop to check if say team[0]'s overall score is greater than every other teams overall score then team[0].name is the highest team with a score of %d because i wont know the score. And so i will then have to check the second highest score and then the third highest and so on down to the lowest. Also , i wont know which team corresponds to the highest score so i will have to create another char string to store the team's name with the highest score and so on
    Last edited by ingeniousreader; 03-06-2012 at 08:51 AM.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    if char a[10] then the address is a not &a.
    so if char *b then
    b = a and not &a, remove the & operator. hope it helps.

    thanks
    satya

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    cheers that helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warning: incompatible pointer type
    By Cnewbi in forum C Programming
    Replies: 7
    Last Post: 12-29-2011, 03:17 PM
  2. How would I fix this incompatible pointer type warning
    By ogglock in forum C Programming
    Replies: 2
    Last Post: 07-11-2011, 01:17 AM
  3. Incompatible Pointer Type Warning
    By kwikness in forum C Programming
    Replies: 5
    Last Post: 10-30-2007, 06:14 PM
  4. warning: assignment from a incompatible pointer type
    By enderandrew in forum C Programming
    Replies: 8
    Last Post: 09-22-2007, 04:07 AM