I'm converting a C Linux program (I don't know what compiler) to Visual C 2019. I'm seeing some strange code.
Today's example is:
long offset = timezone;
I also see:
offset = timezone - 3600;
timezone
appears to be defined as:
struct timezone;
It should be defined as:
struct timezone {
int tz_minutewest;
int tz_dsttime;
};
I don't see any evidence that it is.
It appears that the code is using timezone
as if it is timezone.tz_minutewest
.
Do some compilers do an automatic conversion?
CodePudding user response:
No C compiler should do an automatic conversion from a struct to a long. If one does, it's buggy. (But more likely it's just that the code is buggy, or perhaps someone is playing games with #define
or typedef
somewhere to change the definition of timezone
in an unexpected way)
CodePudding user response:
The struct timezone
you are showing is a Linux/glibc-specific type defined in <sys/time.h>
. See man gettimeofday
.
There is also a variable named timezone
in <time.h>
which is specified by POSIX (X/Open). See man tzset
for information about the Linux/glibc implementation of that feature.
Of course it is not clear whether you are referring to either of these or something else entirely. However in context where the name is used in your example it cannot name a type.
Even if you have both headers included, using timezone
in an expression will refer to the variable, not the type. C allows collision between the name of a type and a non-type. In most contexts the non-type will be preferred if not explicitly prefixed with struct
or similar keywords.
CodePudding user response:
You can run this code in VS by this method.
struct timezone {
int tz_minutewest;
int tz_dsttime;
};
int main()
{
int offset = timezone.tz_minutewest;
// int offset = timezone.tz_dsttime; this is way to call the struct
// by this method you call the data member of structure
return 0;
}