CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Compiler Warning

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

Compiler Warning
PostPosted: Thu Oct 12, 2023 11:07 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Oct 13, 2023 5:57 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Oct 16, 2023 9:09 am     Reply with quote

& should return the actual defined type pointer. So if UTC_Ticks is indeed declared as

Code:

time_t UTC_Ticks;


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

View user's profile Send private message

PostPosted: Tue Oct 17, 2023 12:56 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 17, 2023 8:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 17, 2023 9:36 am     Reply with quote

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. Sad
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Wed Oct 18, 2023 8:27 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group