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

passing pointer array doesn't work

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



Joined: 16 Jan 2008
Posts: 61

View user's profile Send private message

passing pointer array doesn't work
PostPosted: Mon Sep 17, 2012 5:57 pm     Reply with quote

Hi I have following program I am trying to test on PIC16f1937.

Following program works perfectly:
Code:

  char compstr[] =  "|";
  char str[30]='test|string';
  char * pch; 
  char * argv[12], argc=0;

    argv[0] = strtok (str,compstr);
    argc = 0;
     
     while(argc < MAX_ARGC){         
         if(argv[argc] == NULL)
            break;
         argv[++argc] = strtok(NULL, compstr);
     }
     
     //Trap Null Command
     if(argc == 0)
         printf("Command Null Error\n");
     
     while(argc--){
         printf ("\n\r%s",argv[argc]);       
     }
         
     printf("\n\rDone\n");

now when i try to do modular approach with function it doesn't work.
Code:

main(){
  char compstr[] =  "|";
  char str[30]='test|string';
  char * pch; 
  char * argv1[12], argc1=0;

  string_tokenizer(str,compstr,argv1, argc1);
}

int string_tokenizer(char *tokenstring, char *delimiter, char *argv2[], int argc2){
   //char * argv[MAX_ARGC], argc;
   
   //Tokenize the data till end of the string or Max. number of token arguments
   //allowed
   argv2[0] = strtok (tokenstring,delimiter);
   argc2 = 0;
   
   while(argc2 < MAX_TOKEN_ARGUMENTS){     
      if(argv2[argc2] == NULL)
         break;
      argv2[++argc2] = strtok(NULL, delimiter);   
   }
   
   //Trap Token size exceed
   if(argv2[argc2] != NULL)
      return -1;
   
   //Trap Null Command
   if(argc2 == 0)
      return 1;

//testing
   while(argc2--){
      printf ("\n\r%s",argv2[argc2]);       
   }
     
   printf("\n\rDone\n");
   

   return 0;
}
 

Same program I run on the visual C++ platform it does work. Any idea?

Thank you.
_________________
nehal
nehallove



Joined: 16 Jan 2008
Posts: 61

View user's profile Send private message

PostPosted: Mon Sep 17, 2012 6:00 pm     Reply with quote

correct program prints string and test.
_________________
nehal
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue Sep 18, 2012 11:10 am     Reply with quote

Have you read: http://www.ccsinfo.com/forum/viewtopic.php?t=47549 yet?
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 18, 2012 2:29 pm     Reply with quote

I don't believe you tested this program because you have at least one
line in it that doesn't compile, as shown in the program below.

The first error that CCS gives is:
Quote:
Error 5 "pcm_test.c" Line 8(14,16): Character constant constructed incorrectly

That's because you don't have the array init string in double quotes.
Code:

#include <16F1937.H>
#fuses INTRC_IO, NOWDT,BROWNOUT, PUT
#use delay(clock=4M)

//======================================
void main(void)
{
char str[30]='test|string';    // *** This line causes the error

while(1);


Then you say it works in C++. OK, so I compiled the following program
in MSVC++ 6.0. I tried compiling it both as a .C and a .CPP file, and I
got the same error each time:
Quote:
error C2015: too many characters in constant

Again, it wants to see double quotes.
Code:

#include <stdio.h>

//======================================
void main(void)
{
char str[30]='test|string';

while(1);
}

If you make an assertion, we're actually going to check it on here.
We don't fool around. You need to make accurate posts if you want help.
Also post your compiler version.
nehallove



Joined: 16 Jan 2008
Posts: 61

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 1:03 pm     Reply with quote

To PCM Programmer:

Yes you are right. When I wrote the code on forum post I copied wrong version of it. Correct one is double quoted.
Code:

  char compstr[] =  "|";
  char str[20] = "test|string"; 
  char * pch; 
  char * argv1[12], argc1=0;
  char * argv[12], argc=0;

Compiler Version: 4.135

And sorry I am not here to waste anyone's time. I just baffled by the fact that why modular approach didn't work which I wanted to use. I will keep this in mind to keep the post accurate and proof read before posting it.
_________________
nehal
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 1:22 pm     Reply with quote

Can you post the #define statements for these constants ?

MAX_ARGC
MAX_TOKEN_ARGUMENTS
nehallove



Joined: 16 Jan 2008
Posts: 61

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 1:46 pm     Reply with quote

#define MAX_ARGC 8

#define MAX_TOKEN_ARGUMENTS 8
_________________
nehal
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 5:13 pm     Reply with quote

I think this is the same old problem with passing an array of pointers.
There are some threads on this in the archives. I didn't want to go
search for them all. I remembered a previous solution was to make
the function declaration be for an array of int16's, instead of char pointers.
Then it works. You get the output shown below. For this PIC, the
compiler automatically uses 16-bit bit pointers, hence the array of int16's.
Quote:

string

test

Code:
#include <16F1937.h>
#fuses INTRC_IO,NOWDT,NOBROWNOUT,NOPUT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

#include <string.h>

#define MAX_ARGC 8
#define MAX_TOKEN_ARGUMENTS 8

//==========================================
// *** Work-around:   Note the declaration of argv2 is for int16's:

signed int string_tokenizer(char *tokenstring, char *delimiter,  int16 argv2[MAX_ARGC], int argc2)
{
   
//Tokenize the data till end of the string or Max. number of token arguments
//allowed
argv2[0] = strtok (tokenstring, delimiter);

argc2 = 0;
   
while(argc2 < MAX_TOKEN_ARGUMENTS)
  {     
   if(argv2[argc2] == NULL)
      break;
   argv2[++argc2] = strtok(NULL, delimiter);   
  }
   
// Trap Token size exceed
if(argv2[argc2] != NULL)
   return -1;
   
   //Trap Null Command
   if(argc2 == 0)
      return 1;

//testing
   while(argc2--){
      printf ("\n\r%s",argv2[argc2]);       
   }
     
//   printf("Done\r");
   

   return 0;
}
   

//===========================
void main()
{
char compstr[] =  "|";
char str[20] = "test|string"; 
char * argv1[12], argc1=0;

string_tokenizer(str,compstr,argv1, argc1);

while(1);
}
nehallove



Joined: 16 Jan 2008
Posts: 61

View user's profile Send private message

PostPosted: Wed Sep 19, 2012 6:18 pm     Reply with quote

Thank you that works perfectly Smile
_________________
nehal
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