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

Strange "issue" with function prototypes

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



Joined: 17 Sep 2003
Posts: 55
Location: Chester, UK

View user's profile Send private message Visit poster's website MSN Messenger ICQ Number

Strange "issue" with function prototypes
PostPosted: Fri Jan 23, 2004 4:15 am     Reply with quote

Hi all.

Spent an amusing day yesterday tracking down an issue in my code. Compiler version is PCM 3.169.

The sensor I'm working on has FRAM, MMC card, digital I/O and analogue inputs, X10, LCD port etc etc, and a programming/control port.

This port uses commands like

R00000001<cr>
to read the PIC EEPROM location 0.

It wouldn't read above address 000000FF though.

Each function in itself worked. The string was passed to one function, to be parsed, which worked, then to an htoi32, which worked, and returned the value, but at the other end it didn't get through, only as an 8 bit number.

I finally tracked the problem down to the fact that my function prototype was declaring it as

int scan_string...

but it was actually

unsigned in32 scan_string...

Normally, the compiler will complain about function definitions and prototypes being different, but not in this case.

Just FYI!

Pete.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 23, 2004 2:45 pm     Reply with quote

I tested your problem with the latest version of PCM, 3.184,
and found the same problem. The compiler only looks at
the data size specified in the function prototype, and puts
in ASM code based on that. It ignores the return value
size that's specified in the actual function declaration.
In the case below, it only returns an 8-bi value. The
compiler doesn't give an error message about this, even
if +EW is added to the compiler options.

This is the ASM code produced if the function prototype
specifies an "int" as the return value.
Code:
0000                00314 .................... return(retval); 
0010 0830       00315 MOVF   30,W
0011 00F8       00316 MOVWF  78


This is the ASM code produced if the function prototype
specifies an "int32" as the return value.
Code:
0000                00317 .................... return(retval); 
0010 0830       00318 MOVF   30,W
0011 00F7       00319 MOVWF  77
0012 0831       00320 MOVF   31,W
0013 00F8       00321 MOVWF  78
0014 0832       00322 MOVF   32,W
0015 00F9       00323 MOVWF  79
0016 0833       00324 MOVF   33,W
0017 00FA       00325 MOVWF  7A


In both cases, the actual function declaration is specified
to return an "int32".

If I compile a similar program in MSVC++ (substituting "long"
for "int32"), it gives this warning:
c:\msvc\test\test.c(23) : error C2371: 'scan_string' : redefinition; different basic types.
CL returned error code 2.

CCS should do that, too. You should report this to CCS.


#include "16F877.h"
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

int scan_string(char *ptr);

//==========================================
void main(void)
{
int32 value;
char buffer[10];

value = scan_string(buffer);

while(1);
}

//============================================

int32 scan_string(char *ptr)
{
int32 retval;
char c;

c = *ptr;

retval = 0x12345678;

return(retval);
}
Pete Smith



Joined: 17 Sep 2003
Posts: 55
Location: Chester, UK

View user's profile Send private message Visit poster's website MSN Messenger ICQ Number

PostPosted: Sun Jan 25, 2004 5:19 am     Reply with quote

PCM programmer wrote:

If I compile a similar program in MSVC++ (substituting "long"
for "int32"), it gives this warning:
c:\msvc\test\test.c(23) : error C2371: 'scan_string' : redefinition; different basic types.
CL returned error code 2.

CCS should do that, too. You should report this to CCS.


Thanks, I'll do that!

Pete.
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