I have two files(average.h, average.c). In the definition of a structure i have used #ifdef INTERNAL
.
#ifndef AVERAGE_H_
#define AVERAGE_H_
typedef struct str_internal {
int i1;
int i2;
} internal;
typedef struct str_speed {
int lowest;
int highest;
#ifdef INTERNAL
internal i;
#endif
} speed;
extern double average(speed twoStroke);
#endif /* AVERAGE_H_ */
The contents of average.c are,
#include <stdio.h>
#include "average.h"
double average(speed twoStroke) {
double average = (twoStroke.highest + twoStroke.lowest) / 2.0;
#ifdef INTERNAL
average += twoStroke.internal.i1+twoStroke.internal.i2;
#endif
return average;
}
int main() {
speed twoStrokeEngine;
twoStrokeEngine.highest = 70;
twoStrokeEngine.lowest = 50;
#ifdef INTERNAL
twoStrokeEngine.internal.i1=20;
twoStrokeEngine.internal.i2=10;
#endif
double avg = average(twoStrokeEngine);
printf("Average speed of two stroke engine is: %f", avg);
return 0;
}
When i try to compile this code, gcc -DINTERNAL -o average.exe average.c i am getting the following error messages.
average.c:8:22: error: ‘speed {aka struct str_speed}’ has no member named ‘internal’
average += twoStroke.internal.i1+twoStroke.internal.i2;
^
average.c:8:44: error: ‘speed {aka struct str_speed}’ has no member named ‘internal’
average += twoStroke.internal.i1+twoStroke.internal.i2;
^
average.c: In function ‘main’:
average.c:20:17: error: ‘speed {aka struct str_speed}’ has no member named ‘internal’
twoStrokeEngine.internal.i1=20;
^
average.c:21:17: error: ‘speed {aka struct str_speed}’ has no member named ‘internal’
twoStrokeEngine.internal.i2=10;
The problem is with
twoStroke.internal.i1+twoStroke.internal.i2;
^^^ ^^^^
internal
is a type, not a member that you can access. You need to access the member variables. The member variable, which is of type internal
is i
, so your statements should look something like
twoStroke.i.i1+twoStroke.i.i2;