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 support@ccsinfo.com

Pointer error...

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



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

Pointer error...
PostPosted: Thu Oct 14, 2021 12:47 am     Reply with quote

This is only an example not production code.

Problem is in 5.105 not in 5.066. I use PCW IDE compiler.
Problem is sending a pointer for an array to a function. In 5.066 i use int8 and it work, in 5.105 it only work when using int16.

See my test code, it is a little long but simple.

-Is it a bug in 5.105?

a google search found this:
https://www.includehelp.com/c-programs/pass-an-array-of-strings-to-a-function.aspx

Code:

/*
This is only a test program. I use PCW IDE as compiler.
Code are tested in MPLAB 8.92 as simulation, and in real hardware.
It is also tested in Code::Blocks too and work.

Problem is sending a array to a function.
In 5066 it work when using int8
In 5105 it must use int16 to work. -Think this is an error.
*/

#include <18F26K22.h>

#use delay(clock=8M,int)
#use RS232(Baud=115200,STOP=1,PARITY=N,BITS=8,UART1,ERRORS)

#include "string.h"

//in 5105 assign must be int16.
//in 5066 int8/char work ok.
void PointerTest(char **str){
 int8 i;
 
 //Print the token string
 for (i=1; i<6; i++){
  printf("PT   *str:[%u] %s\r\n",i,str[i]);
 }
}

void main(){
 //string array
 char *buffer[7]={"10","11","12","13","14","15","16"};

 printf("Start\r\n");
 PointerTest(buffer);
 printf("End\r\n");
}


Last edited by hmmpic on Thu Oct 14, 2021 5:13 am; edited 4 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Oct 14, 2021 1:38 am     Reply with quote

There are several syntactical 'wrongs'. In C the name of an array is the
pointer to it. so &buffer is really wrong. Just use buffer.
Adding the &, doesn't generate a pointer to this. You would have to have
a variable, that contains this address, which you can then pass the address
of, to be a pointer to a pointer.
In C, & array_name, is exactly the same as array_name/
What is being passed, is a pointer to a character, not a pointer to a
pointer (which ** implies).
So you are posting code that shouldn't really work. It was a fluke it did
before, and it is still really a fluke it does now with int16....
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Thu Oct 14, 2021 2:25 am     Reply with quote

I made a lot of test on this, there are a problem in 5105. I adjust my example to only the minimum.

Problem is there in 5105. Why is it not working as an char/int8 pointer but need a int16 when assigning the pointer.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Oct 14, 2021 6:57 am     Reply with quote

OK.
The new version makes good sense (though remember the first entry
is 0, not 1 in the array).

I'd raise this with CCS. What is happening, is that the maths for the
array index, is being incremented by the target size, not the pointer
size. Very wrong. It does the same if you use the syntax: char * str[]
instead of the double pointer.

However it does work correctly if you use a typedef:
Code:

/*
This is only a test program. I use PCW IDE as compiler.
Code are tested in MPLAB 8.92 as simulation, and in real hardware.
It is also tested in Code::Blocks too and work.

Problem is sending a array to a function.
In 5066 it work when using int8
In 5105 it must use int16 to work. -Think this is an error.
*/

#include <18F26K22.h>

#use delay(clock=8M,int)
#use RS232(Baud=115200,STOP=1,PARITY=N,BITS=8,UART1,ERRORS)

#include "string.h"
//generate a character pointer typedef.
typedef char* cptr;

//in 5105 assign must be int16.
//in 5066 int8/char work ok.
//now declare this as receiving an array of character pointers
void PointerTest(cptr str[])
{
 int8 i;
 
 //Print the token string
 //Arrays start at 0, not 1. Changed.
 for (i=0; i<7; i++)
 {
  printf("PT   *str:[%u] %s\r\n",i,str[i]);
 }
}

void main()
{
 //string pointer array using typedef.
 cptr buffer[]={"10","11","12","13","14","15","16"};

 printf("Start\r\n");
 PointerTest(buffer);
 printf("End\r\n");
}


This makes it work.
It looks as if the typedef here makes the code 'know' that the object
passed is a pointer, and increment by the typedef size.
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Thu Oct 14, 2021 7:18 am     Reply with quote

Thanks.

I have reported this to CCS with my latest example.
Hope they only fix the problem, and not add 2 new problem to the next release:-)
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Oct 15, 2021 12:34 am     Reply with quote

A little update on this. Decided to investigate a bit more.

Using the posted declaration on 5.066, if you read the sizeof(str[i]) in the
loop, it gives 2. Yet the array increment is by 1.
With the typedef, it gives 2, and increments by 2 in all versions.
All of the compilers actually give a warning 'pointer types do not match' with
the code as posted. Again this disappears using the typedef. It has already
gone wrong, when you get to 5.080. 5.078, is the last version I have found
where it works.
Declaring the function with PointerTest(char *str[]) gets rid of the pointer
types warning, but does not affect the operation.

5.080 has the following note in it's 'changes' entry:

5.080 A bug involving passing arrays to an inline function for some chips is fixed.

It looks as if this fix is actually what causes the problem....
However it does the same if PointerTest is declared as #separate.
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