# 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. ## 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);
}```

2. 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