Nickson
Nickson

Reputation: 135

My C expression must have struct or union type

I have a method that is intended to print out values as per other methods defined. I will post the methods directly involved to reduce the code. However, Visual Studio returns the error

expression must have struct or union type

My code is as below:

int i;
double cat[12];
#define MAX 3
#define GRAMS 64

struct CatFoodInfo {
    int SKU[MAX], CalPerServ[MAX];
    double price[MAX], lbs[MAX], kg[MAX], grams[GRAMS], servings[MAX], costPerServ[MAX], costPerCal[MAX];
} CatFoodInfo;

void displayReportData() {
    for (i = 0; i < MAX; i++) {
        int sku = cat[i + (3 * i)];
        double price = cat[i + (1 + (3 * i))];
        double lbs = cat[i + (2 + (3 * i))];
        int calPerServ = cat[i + (3 + (3 * i))];
        int kg = cat[kg];
        printf("%07d %10.2lf %10.1lf %10.4lf %9d %8d %8.1lf %7.2lf %7.5lf", sku, price, lbs, kg, cat.grams[i], calPerServ, cat.servings[i], cat.costPerServ[i], cat.costPerCal[i]);

    }
}

The above code is will return that error. I have tried changing the values on the printf from cat.grams[i] to CatFoodInfo.grams[i]. This gets rid of the error but only zero is returned.

What is the correct way around this or where am I going wrong?

Upvotes: 0

Views: 699

Answers (1)

Jose
Jose

Reputation: 3460

It isn't quite clear what's your purpose.

cat is an array of doubles, it has no idea about the struct CatFoodInfo or its fields, hence cat.grams[i] doesn't make sense. For that, cat should be a struct of type CatFoodInfo, e.g.:

typedef struct { 
   ...
} CatFoodInfo;

CatFoodInfo cat;

But this change the meaning, now cat isn't an array, there is only one element of type CatFoodInfo, so you will have problems with the other parts of the code (e.g. cat[kg]).

However, to treat cat as an array with CatFoodInfo elements, you need:

typedef struct { 
   ...
} CatFoodInfo;

CatFoodInfo cat[12];

But again, you would have to rewrite most of your code, e.g. cat.grams[i] to cat[j].grams[i].

Upvotes: 1

Related Questions