c - Char array of array values to pointer array -


i need put array of array chat values in pointer array. first used code , works me.

char *current_tag_lists[20]; char current_tag_list1[]="0005808897"; char current_tag_list2[]="0009953997"; char current_tag_list3[]="0000116600";  current_tag_lists[0] = current_tag_list1; current_tag_lists[1] = current_tag_list2; current_tag_lists[2] = current_tag_list3; 

so can access value index current_tag_lists[0].

but actual requirement add these value in run time follows. example code.

char *current_tag_lists[20];  while(k<6)  {     char rfid_input_characters[12];     while(a<13){         if(a==12){             current_tag_lists[k]=rfid_input_characters;             a=0;             k++;             break;         }         else{             rfid_input_characters[a]='a'; // example in code value             a++;        }      }    } 

but problem "current_tag_lists" not store values. store current value. every time replace previous value. need keep values above example , need access index (current_tag_lists[0]).

can please me. actual code.

while(k<6)//while(!(c1==1)) {     char rfid_input_characters[12]={0};     while(a<14){           if (a == 0) {             receivebyteserially();             a++;         }          else if (a == 13 ) {             receivebyteserially();             current_tag_lists[k] = malloc(strlen(rfid_input_characters) + 1);             strcpy(current_tag_lists[k], rfid_input_characters);             lcd_set_cursor(1,1);             lcd_write_string(rfid_input_characters);             lcd_set_cursor(2,1);              lcd_write_string(current_tag_lists[k]);             = 0;             k++;             break;         }         else if(k%2!=0 && a==1){              char x=receivebyteserially();              if((x!=0x02)&&(x!=0x03)){                 a++;              }         }         else{             char x=receivebyteserially();              if((x!=0x02)&&(x!=0x03)){                  if(k%2 !=0){                      rfid_input_characters[a-2] = x;                  }                  else if(a<12){                 rfid_input_characters[a-1] = x;                  }                 a++;         }         }     }  } 

please if(a==13).

this error log.

c:\program files (x86)\microchip\xc8\v1.33\sources\common\strcpy.c:19:     error: (1466) registers  unavailable code generation of expression  (908) exit status = 1  make[2]: ***      [dist/default/production/super_smart_backpack.x.production.hex] error 1  make[1]: *** [.build-conf] error 2  make: *** [.build-impl] error 2` `nbproject/makefile-default.mk:119: recipe target  'dist/default/production/super_smart_backpack.x.production.hex' failed make[2]: leaving directory 'f:/irushi-final/super smart backpack.x/super   smart backpack.x' nbproject/makefile-default.mk:78: recipe target '.build-conf' failed make[1]: leaving directory 'f:/irushi-final/super smart backpack.x/super smart backpack.x' nbproject/makefile-impl.mk:39: recipe target '.build-impl' failed  build failed (exit value 2, total time: 1s) 

given posted code:

char *current_tag_lists[20];  while(k<6)  {     char rfid_input_characters[12];     while(a<13){         if(a==12){             current_tag_lists[k]=rfid_input_characters;             a=0;             k++;             break;         }         else{             rfid_input_characters[a]='a'; // example in code value             a++;        }      }    } 

there several details need attention:

1) need initialize current_tag_lists[] nulls make easy when later malloc'd strings need passed free()

char *current_tag_lists[2] = { null }; 

2) each string needs unique place stored:

char *temp =null; if( null == (temp = malloc( 12 ) ) ) { // malloc failed     perror( "malloc failed" );     cleanup();  // frees malloc'd areas     exit( exit_failure ); }  // implied else, malloc successful 

regarding line:

while( < 13 ) 

the max number of characters per entry (per original code , above mallocf() 12. c references offsets arrays 0...(array len -1) 12 (a<13) accessing beyond upper bound of array. leading undefined behaviour, can/will lead seg fault event.

suggest following code:

#include <stdlib.h>  // exit, exit_failure #include <string.h>  // malloc, free  #define max_tag_lists (20) #define max_rfid_len (12)  char *current_tag_lists[ max_tag_lists ];  // initialize current_tag_lists[] make cleanup simple memset( current_tag_lists, '\0', sizeof(current_tag_lists));  char *temp =null; for( int k = 0; k < max_tag_lists; k++ ) {     if( null == (temp = malloc( max_rfid_len ) ) )     { // malloc failed         perror( "malloc failed" );         cleanup( current_tag_lists );  // frees malloc'd areas         exit( exit_failure );     }      for( int = 0; < max_rfid_len; a++ )     {          temp[a]='a'; // example in code value     } // end      current_tag_lists[k]=temp;     temp = null; } // end    void cleanup( char *current_tag_lists ) {     int i;     for( = 0; < max_tag_lists; i++)     {         // note: ok pass null free         free( current_tag_lists[i] );     } } // end function: cleanup 

Comments

Popular posts from this blog

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -