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

Passing 2D character array to function

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



Joined: 31 Dec 2010
Posts: 39

View user's profile Send private message

Passing 2D character array to function
PostPosted: Thu Oct 06, 2011 2:14 am     Reply with quote

Hello,

I am trying to pass 2D character arrays to a function, then step through the values. But, it's not working right.

Am I on the right track? Any help is appreciated.

Code:

#include <18F4550.h>
#device ICD=TRUE
#device PASS_STRINGS=IN_RAM

#fuses HSPLL,PLL4,NOLVP,NOWDT,PUT,ICSP1

#use delay (clock=20000000)

#use RS232(DEBUGGER)

char speed[3][7]={"Slow", "Medium", "Fast"};
char direction[4][6]={"Up", "Down", "Left", "Right"};

void array_print( char *list, int count )
{
   char *ptr;
   int8 i;
   
   *ptr = list;
   
   for (i=0; i<count; i++)
   {
      printf("%s\n\r",*ptr);
      ptr++;
   }
}

void main()   
{   
   array_print( speed, 3 );
   array_print( direction, 4 );
   
   while (TRUE)  {}
}
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Oct 06, 2011 3:40 am     Reply with quote

*ptr = list; // Changes what the pointer points to.

ptr = list; // This is what I think you need. Change the pointer itself.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Thu Oct 06, 2011 4:41 am     Reply with quote

It is also worth realising that you don't have to have a second copy of the pointer inside the function.

Inside array_print, 'list', is a _local copy_ of the value passed to the function. So there is no need to copy this to 'ptr'. Just use it.

However there is a problem in terms of what you point to.

Start with speed. An array of three seven element string objects. So using '0' to indicate a null, and 'x' to indicate undefined, in memory:

Slow0xxMedium0Fast0xx

Now, 'speed' points to the first element in this, and inside array_print, so does 'list'. You call printf, and "Slow" gets printed, then increment ptr, or list (whichever you use), and it now points to the 'l', moving forward by one character.
Problem is that the pointer does not know about the size of the elements in the array, just knowing it points to 'characters', so when you increment it, it moves to the next _character_, not the next 'string'.....

In C, there are two ways of doing what you want. The first is if the arrays all have the same size rows, and your function can then be told this with a declaration like

char list[][7]

So the function knows the pointer is to an array, and to move forward by seven elements for the next row, or to pass the size of the row to the function, as:

Code:

#include <18F4550.h>
#device ICD=TRUE
#device PASS_STRINGS=IN_RAM

#fuses HSPLL,PLL4,NOLVP,NOWDT,PUT,ICSP1

#use delay (clock=20000000)

#use RS232(DEBUGGER)

char speed[3][7]={"Slow", "Medium", "Fast"};
char direction[4][6]={"Up", "Down", "Left", "Right"};

void array_print( char *list, int count ,size) {
   int8 i;
   for (i=0; i<count; i++) {
      printf("%s\n\r",*list);
      list+=size;
   }
}

void main(void) {
   array_print( speed, 3 ,sizeof(speed)/3);
   array_print( direction, 4 sizeof(direction)/4);   

   do {
   } while(TRUE); //This avoids the compiler error message 
}


Best Wishes
pebbert9



Joined: 31 Dec 2010
Posts: 39

View user's profile Send private message

PostPosted: Thu Oct 06, 2011 8:08 am     Reply with quote

thank you
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