Why "pointer expected"

This is a discussion on Why "pointer expected" within the C Programming forums, part of the General Programming Boards category; Please help to check Code: struct remittance { char country[100]; int total; int highest; int lastYearAmount; struct remittance* left; struct ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Hong Kong
    Posts
    13

    Why "pointer expected"

    Please help to check

    Code:
    struct remittance 
    { 
       char country[100]; 
       int total;
       int highest;
       int lastYearAmount;
       struct remittance* left; 
       struct remittance* right; 
    };
    struct topFiveCountry
    {
       char country[100];
       int lastYearAmount;
    };
    struct topFiveCountry topFiveCountryResult[5];
    void findTopFiveCountry(struct remittance* treeRoot, struct topFiveCountry* highestArray)
    {
         int i, j;   /* loop counter */
         if (treeRoot == NULL)
      {
            return;
      }
         if (treeRoot->lastYearAmount > highestArray + 4->lastYearAmount)
         {
             for (i = 0; i < 5; i++)
             {
                 if(treeRoot->lastYearAmount > highestArray + i->lastYearAmount)
                 {
                     for(j = 3; j >= i; j--)
                     {
                         strcpy (highestArray + j->country, highestArray + j + 1 -> country);
                         highestArray + j->lastYearAmount = highestArray + j + 1 -> lastYearAmount;
                     }
                     strcpy (treeRoot->country, highestArray + i->country);
                     treeRoot->lastYearAmount = highestArray + i->lastYearAmount;
                             
                     break;
                 }
             }
         }
         findTopFiveCountry(treeRoot -> left, topFiveCountryResult);
         findTopFiveCountry(treeRoot -> right, topFiveCountryResult);
    }
    Last edited by Oscar Wong; 06-17-2012 at 02:28 AM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Any complicated expression such as highestArray + i or highestArray + j + 1 must be done first so that the pointer is dereferenced correctly. You ensure this happens with grouping. For example,
    highestArray[j + 1].country
    The brackets ensure that the sub-expression j + 1 is properly evaluated before the element is accessed. Since you are using arrays I recommend this syntax be used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 11-08-2010, 12:47 AM
  2. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  3. Replies: 24
    Last Post: 09-06-2006, 06:17 PM
  4. "expected primary-expression before "else" ???
    By Helix Stark in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21