|
|
View previous topic :: View next topic |
Author |
Message |
rovtech
Joined: 24 Sep 2006 Posts: 262
|
Pointer types do not match |
Posted: Tue Nov 22, 2016 3:24 pm |
|
|
A program that worked in 2013 now gives "Pointer types do not match" warnings every occurrence of:
I have edited the program to the relevant parts as below:
CCS PCM C Compiler, Version 5.055
Code: | /* Pre-processor directives */
#include <16F884.H>
#include <math.h>
#fuses INTRC_IO, NOWDT, PUT, NOPROTECT, BROWNOUT, MCLR
#use delay (clock=1000000)
#use I2C (master, SCL=PIN_C3, SDA=PIN_C4)
#byte portc = getenv("SFR:PORTC")
// define I2C address
#define LCD_WRT_ADDR 0X4E // LCD display
#define buff_size 22 // characters per line plus one
// Function prototypes
void clear_LCD (void); // Clear LCD
void text_position (int line, int column); // set start for next text
void set_font (char size); // set font size
void send_str(char buff[buff_size]); // Send string to LCD
// The main function
void main(void)
{
// setup ports
set_tris_c (0x00); // all outputs
// declare variables
char buff[buff_size];
// Start the alphanumeric display sequence
clear_LCD(); // clear the LCD
sprintf(buff, "Graphics Demo"); // place text string in buffer
send_str(buff); // display text array
delay_ms(2000); // for 2 seconds
text_position(0,1); // start second line
set_font(10); // change text to size 10
sprintf(buff, "123456789012345678901"); // place text string in buffer
send_str(buff); // display text array
delay_ms(2000); // for 2 seconds
text_position(0,3); // start of 4th line
sprintf(buff, "disp floating point"); // place text string in buffer
send_str(buff); // display text array
} // end of main function
// Functions
// Clear Display
void clear_LCD (void)
{
I2C_START (); // start I2C
I2C_WRITE (LCD_WRT_ADDR); // addr of LCD
I2C_WRITE ('C'); // C CL to clear display
I2C_WRITE ('L'); // L
I2C_STOP (); // stop I2C
}
// set position of next text
void text_position(int line, int column)
{
I2C_START (); // start I2C
I2C_WRITE (LCD_WRT_ADDR); // addr of LCD
I2C_WRITE ('T'); // T, TP set text position
I2C_WRITE ('P'); // P
I2C_WRITE (line); // line position
I2C_WRITE (column); // column position
I2C_STOP (); // stop I2C
}
// Set Font Size
void set_font (char size)
{
I2C_START (); // start I2C
I2C_WRITE (LCD_WRT_ADDR); // addr of LCD
I2C_WRITE ('S');
I2C_WRITE ('F');
I2C_WRITE (size);
I2C_STOP (); // stop I2C
}
// send string to LCD
void send_str(char buff[buff_size])
{
int i;
I2C_START (); // start I2C
I2C_WRITE (LCD_WRT_ADDR); // addr of LCD
I2C_WRITE('T'); // send TT for text coming
I2C_WRITE('T');
for (i=0; i<buff_size; i++)
{
I2C_WRITE(buff[i]); // start with a Z
}
I2C_WRITE(0);
I2C_STOP (); // stop I2C
}
// end
|
The program works but no longer displays "Graphics Demo", it displays the rest (which for the full program are various graphics). This may be another issue from using a different PIC. The original PIC works fine.
My concern is what the mis-matched pointer types means. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 22, 2016 6:18 pm |
|
|
I was able to remove the warning by changing the function prototype
to just a simple pointer declaration as shown below in bold:
Quote: |
void send_str(char *buff); // Send string to LCD
void send_str(char *buff)
{
printf("%s", buff);
}
|
Also you didn't show the send_str() function so I made one, and I added
a #use rs232() statement for the printf. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19592
|
|
Posted: Wed Nov 23, 2016 2:13 am |
|
|
It's worth perhaps expanding a little.
This was a change done about ten versions ago in the compiler. Prior to this, if the pointer types did not match, there was no complaint. Now it moans.
Quite sensible, since it makes you aware that the sizes of the objects involved may not be the same. However it is a little 'dumb' (so doesn't realise that - for instance, a 'char', and an 'int8' are the same size on the PIC16/18). On the first few versions, there was also not an exception for the 'void *' type (which most compilers allow you to use as a 'get out of jail free' card for passing different size pointers). The latest compilers do now have this exception.
So (for instance), imagine you have a function that wants to handle data as int8, but want to give it data from a variety of other types.
So:
Code: |
//declare a function to fill a variable with zeros, handled by a void pointer
void test(void * ptr, int8 size)
{
do //fill the variable with zeros
{
*(int8 *)ptr++=0; //now make the pointer the size you want
} while (--size); //for the number of bytes involved
}
//Then you can call like:
int16 fred;
int32 dick;
test(fred, sizeof(fred));
test(dick, sizeof(dick));
|
Here the same function, accepts a pointer to an int16, and an int32, without complaint, by using the 'void *' type, and then casting the pointer to the type required inside the function.
If you want the 'older' behaviour, you can simply suppress this warning. However better by far to make sure that types are the same, or are 'flexible' as shown.
What is interesting in your problem is that the compiler is treating char[] as a different type to char *. These are both totally equivalent. It is just the tester is looking for one, and not matching the other.....
However It may be complaining because you are trying to give it a size. In C, when you pass a pointer to a function it does not carry the object size with it (hence my use of 'sizeof'). You are then trying to give this pointer a size limit in the function. This is generally not supported in C. C++, and some of the derivative languages (particularly on processors that support hardware bounds checking), do support the target function array having a size, but C as standard does not. |
|
|
rovtech
Joined: 24 Sep 2006 Posts: 262
|
|
Posted: Wed Nov 23, 2016 11:04 am |
|
|
Thanks PCM Programmer
my send_str function should be shown near the end of the functions.
I tried in every occurrence
Code: | void send_str(char *buff[buff_size]);
send_str(*buff);
|
It gets rid of the compile warnings but I get garbage unless I change the sprintf as well in every occurrence as below
Code: | sprint(*buff,"xxxxx");
send_str(*buff);
|
even with this the first display is missing "Demo" and shows "Graphics" only.
So it looks like the compiler is getting very confused with pointers as am I.
The original program without the * works well, it just gives the warnings.
Thanks Ttelmah I am still trying to understand what is happening. I thought the compiler recognized an array and took care of the pointers.
I cannot see where I am using different types. I am using a fixed length array, and doesn't sprintf take care of the null termination? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1358
|
|
Posted: Wed Nov 23, 2016 11:18 am |
|
|
rovtech wrote: | Thanks PCM Programmer
my send_str function should be shown near the end of the functions.
I tried in every occurrence
Code: | void send_str(char *buff[buff_size]);
send_str(*buff);
|
It gets rid of the compile warnings but I get garbage unless I change the sprintf as well in every occurrence as below
Code: | sprint(*buff,"xxxxx");
send_str(*buff);
|
|
That wasn't the suggestion. The suggestion was:
Code: |
void send_str(char *buff);
send_str(buff);
|
|
|
|
rovtech
Joined: 24 Sep 2006 Posts: 262
|
|
Posted: Wed Nov 23, 2016 12:23 pm |
|
|
Yes that works and makes a lot more sense. Thanks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19592
|
|
Posted: Wed Nov 23, 2016 3:06 pm |
|
|
It's also work perfectly well as:
Code: |
void send_str(char buff[])
|
A I have already said, the problem is coming because you are trying to say that the pointer passed to the function, has a 'size' associated with it. It doesn't. It is just a pointer. A pointer passed to a function has no concept of the size of the object being pointed 'to', and the compiler is complaining because you are trying to associate a size with it.
Some later compiler (C99 and later), support the declaration of a size as:
Code: |
void send_str(char buff[static size_val])
|
but CCS does not support this. |
|
|
rovtech
Joined: 24 Sep 2006 Posts: 262
|
|
Posted: Thu Nov 24, 2016 12:37 pm |
|
|
Thanks everyone. I now understand and everything works without warnings. |
|
|
|
|
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
|