View previous topic :: View next topic |
Author |
Message |
JAM2014
Joined: 24 Apr 2014 Posts: 138
|
Compiler Warning |
Posted: Thu Oct 12, 2023 11:07 am |
|
|
Hi All,
I'm getting a compiler warning "pointer types do not match" on the following line of code:
Code: | ctime(&UTC_Ticks, tUTCString);
|
This code is working, converting UTC ticks to a textual date/time string.
I think the issue is the '&' character. Removing it makes the warning go away, but the code stops working!
This is PCM, v5.110
Thanks,
Jack |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Fri Oct 13, 2023 5:57 am |
|
|
The warning is because the pointer types don't match!...
& returns a default pointer type. The ctime function want a a pointer to
a time_t type. Now it works because the pointer gets cast inside the
function, but the way to avoid the warning is to do this yourself:
Code: |
ctime(((time_t *)&UTC_Ticks, tUTCString);
|
The modern compilers have got more and more explicit at giving warnings
like this to ensure that you really do 'mean' what you are asking it to do.
Requires you to more and more 'explicitly' force type casts like this. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Mon Oct 16, 2023 9:09 am |
|
|
& should return the actual defined type pointer. So if UTC_Ticks is indeed declared as
Then I would send a bug report to CCS support (via email or phone...they don't regularly check the forums). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Tue Oct 17, 2023 12:56 am |
|
|
No. In C++, & is required to return a pointer of the type of the variable,
but in C, this is not required. Hence the warning. It is only a warning
though not an error as such. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Tue Oct 17, 2023 8:54 am |
|
|
That is incorrect, the C standard states:
Section: 6.5.3.2 Address and indirection operators
Quote: |
The unary & operator yields the address of its operand. If the operand has type ‘‘type’’, the result has type ‘‘pointer to type’’.
|
The & operator does indeed return a typed pointer in C. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Tue Oct 17, 2023 9:36 am |
|
|
I slightly disagree.
That was a change between the original C programming language book, and
the ANSI C edition. There was an appendix added which stated this for the
ANSI edition.
Now CCS normally complies with the original K&R wording, rather than
ANSI wording.
However a quick test shows that if the UTCtime variable is correctly declared
as a time_t type, the compiler does not give the warning he describes
(using his version). So I suspect his UTCtime variable is not actually
declared as time_t, which is why he gets the error. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1345
|
|
Posted: Wed Oct 18, 2023 8:27 am |
|
|
Yeah, that's most likely it (probably not the correct type).
For reference, the section I quoted came out of the ISO standard, not the C book. CCS has been trying to be more ANSI compliant, so if the OP is actually declaring UTCtime as the correct type and the compiler is getting confused and issuing that warning due to a parsing bug, then it would be good for them to at least shoot an email to CCS support with the code attached so they can decide if they are ok with it or not. |
|
|
|