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

array of pointers

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







array of pointers
PostPosted: Tue May 27, 2008 1:33 pm     Reply with quote

Can someone take a quick look and tell me what I'm missing?

The output is:

line=test1:test2:test3
data0=test1
data1=·}ç }? × þû¿ýÞûþ ÷¿ßýö ?ßï ÛÞî÷׿ }¿ _ ·ú÷þß» ÷§¿ßŸ û î¿ 
data2=ß


Code:

#include <16F88.h>
#fuses HS,NOWDT,NOLVP,PUT,NOPROTECT,BROWNOUT,NOWRT
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B2, ERRORS)
#include <string.h>
#include <stdlib.h>

void get_data(char **data, char *line)
{
    int i = 0;
    char sep[2];
    strcpy(sep, ":");
    data[i] = strtok(line, sep);
    for (i = 1; i < 3; i++)
    {
        data[i] = strtok(0, sep);
    }
}

void update()
{
    int x = 0;
    char line[32];
    char *data[3];
    strcpy(line, "test1:test2:test3");   
    printf("line=%s\n\r", line);
    get_data(data, line);
    for (x = 0; x < 3; x++)
    {
        printf("data%u=%s\n\r", x, data[x]);
    }
}

void main()
{
    while (1)
    {
        update();
        delay_ms(1000);
    }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 27, 2008 2:10 pm     Reply with quote

Post your compiler version.
cheezy
Guest







PostPosted: Tue May 27, 2008 2:33 pm     Reply with quote

sorry

4.057
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 27, 2008 3:04 pm     Reply with quote

I installed your version and compiled it and ran in the MPLAB simulator
and it displayed this in the output window:
Quote:

line=test1:test2:test3

data0=test1

data1=test3

data2=
cheezy
Guest







PostPosted: Tue May 27, 2008 6:20 pm     Reply with quote

That is not what I expect to see out. Should be:

Quote:

line=test1:test2:test3
data0=test1
data1=test2
data2=test3


I think I'm missing something simple.

I'm not familiar with the simulator. I'll try to figure out how to use the simulator and see if I can reproduce your results. Maybe that will help me understand what I'm doing wrong.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 27, 2008 7:46 pm     Reply with quote

Read this post for instructions on how to setup the MPLAB simulator so it
displays the output of the PIC's hardware UART in the Output window:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&start=1
This feature of MPLAB makes it really easy to debug programs with
the simulator. You can view the output of putc() and printf() statements
in the Output window in MPLAB. You don't even need hardware.
cheezy
Guest







PostPosted: Tue May 27, 2008 8:44 pm     Reply with quote

thanks a bunch. I figured out how to set it up and that makes things much faster. I was getting tired of programming it then switching over and looking at the output with hyperterminal.

I got the same thing as you in the simulator:

Quote:

line=test1:test2:test3

data0=test1

data1=test3

data2=



Is a bit different than what I see in hyperteminal but is still -not- the expected output. Any idea why the output is incorrect?
cheezy
Guest







PostPosted: Wed May 28, 2008 9:35 am     Reply with quote

I messed around with this code last night and still couldn't figure out what is wrong. My understanding up pointers to pointers is not so good which doesn't help things. If you happen to figure it out please let me know as I'm stuck.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 28, 2008 12:27 pm     Reply with quote

The first thing I did was to strip your program down and get rid of all
the function calls. Then it works. Here's the output:
Quote:

data0=test1
data1=test2
data2=test3

To debug this further, I would put back in the function call to get_data()
or a truncated version of get_data(). If it fails then I would start to
carefully study what's happening. This includes writing further test
programs to study the behavior of the parameter passing for char**.
It includes looking at the .LST file. Hopefully you will do this.

Here's the test program that produced the output shown above:
Code:

#include <16F88.h>
#fuses HS,NOWDT,NOLVP,PUT,NOPROTECT,BROWNOUT
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B2, ERRORS)
#include <string.h>

//===========================
void main()
{
int8 i;
char line[32] = {"test1:test2:test3"};
char *data[3];
char sep[2] = {":"};

i = 0;
data[i] = strtok(line, sep);

for(i = 1; i < 3; i++)
   {
    data[i] = strtok(0, sep);
   }

for(i = 0; i < 3; i++)
   {
    printf("data%u=%s\n\r", i, data[i]);
   }

while(1);
}
cheezy
Guest







PostPosted: Wed May 28, 2008 10:14 pm     Reply with quote

I originally trimmed down the bulk of the code to demonstrate the problem (as you have suggested in many other posts). However, I didn't remove the functions until you suggested doing so. After doing so, I got it to work just as you said.

I will play around with it some more and see if I can figure it out. If not, I will just simplify the code and stay away from passing char **.

I really appreciate you taking the time to help me out. thanks
Ttelmah
Guest







PostPosted: Thu May 29, 2008 3:15 am     Reply with quote

It appears from a quick glance at the listing, that the compiler is only adding the index 'i' to the array address, not doubling it (as is required since the array holds pointers), before the addition...

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 29, 2008 4:31 pm     Reply with quote

Here's a possible work-around. Since CCS has trouble doing pointers
to pointers, just give it an array of unsigned integers instead.
Changes to your program:
1. In main(), declare 'data' as an array of unsigned 8-bit integers
instead of pointers.
2. In the get_data() declaration, declare the first parameter as
in the same way.

If you want to have access to all the RAM in the PIC, then add the
#device *=16 statement after the #include line.
Then change both of the array declarations to int16 instead of int8.
Changes are shown in bold below:
Quote:

#include <16F88.h>
#fuses HS,NOWDT,NOLVP,PUT,NOPROTECT,BROWNOUT,NOWRT
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B2, ERRORS)
#include <string.h>
#include <stdlib.h>

void get_data(int8 *data, char *line)
{
int i = 0;
char sep[2];
strcpy(sep, ":");
data[i] = strtok(line, sep);
for (i = 1; i < 3; i++)
{
data[i] = strtok(0, sep);
}
}

void update()
{
int x = 0;
char line[32];
int8 data[3];
strcpy(line, "test1:test2:test3");
printf("line=%s\n\r", line);
get_data(data, line);
for (x = 0; x < 3; x++)
{
printf("data%u=%s\n\r", x, data[x]);
}
}

void main()
{
while (1)
{
update();
delay_ms(1000);
}
}
cheezy
Guest







PostPosted: Thu May 29, 2008 11:01 pm     Reply with quote

I had decided to go with using globals to deal with the problem. But Now, I've implemented your suggestion and tested. Works perfectly! Maybe this will also help someone else.

thanks again
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