Question

If I declare and never use variable then gcc will give me a warning.

But if I have a struct with several members and some of those are not used, gcc will not warn about it...

Is there an option (or another method) to check these?

(Of course I can manually delete some entries and try to compile again, but I am looking for this kind of approach).

Thanks

Was it helpful?

Solution

No GCC won't warn about this. Mostly because in the majority case whether or not a member is used can't be determined. A good portion of struct are defined in a header file. This can be used by not just your application but by any other application referencing your .lib or using the same header file. Hence just because the current piece of code being compiled doesn't use the member it doesn't mean that the member is not used by some other piece of code.

Local variables are different. Whether or not they are used easily determined by compiling only the function in question. Hence GCC, and many other compilers, give a warning.

OTHER TIPS

You can legally and portably access the first member of a struct without using its name, simply by casting the struct to the type of the first member.

typedef struct {
    int x;
} mystruct;

mystruct s;
*(int*)&s = 3;

You can also non-portably, but with virtually 100% reliability, access any field in the struct without using its name by casting the struct to another struct type with a compatible structure.

typedef struct {
    int x;
    char y;
} mystruct;

typedef struct {
    int a;
    char b;
} otherstruct;

mystruct s;
((otherstruct*)&s)->b = 'C';

I'm afraid that this means that neither searching the source for the field's name, nor removing it, are completely reliable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top