Char array of array values to pointer array

494 views Asked by At

I need put array of array chat values in to pointer array. for that first I used code like this and it works for 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 I can access the value by index current_tag_lists[0].

But my actual requirement is to add these value in run time as follows. This is a 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'; // this example in my code it get value like this
            a++;
       }
     }
   }

But the problem is that "current_tag_lists" does not store all the values. it only store the current value. It every time replace the previous value. i need to keep the values as my above example and need to access from index (current_tag_lists[0]).

Can anyone please help me. This is my 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]);
            a = 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 only look the if(a==13).

This is my error log.

C:\Program Files (x86)\Microchip\xc8\v1.33\sources\common\strcpy.c:19:     error: (1466) registers  unavailable for code generation of this 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 for 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 for 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 for target '.build-impl' failed

BUILD FAILED (exit value 2, total time: 1s)
4

There are 4 answers

17
AudioBubble On

This is how you can do it:

If I got you correctly.

// First you create a normal null terminated string and copy some test string to it
char RFID_input_characters[12]="test";
....
// Now since you have array of pointers, you can allocate some space 
// and set k-th pointer point to it
current_tag_lists[k] = malloc(strlen(RFID_input_characters) + 1);

// Now you can copy something to the memory you allocated
strcpy(current_tag_lists[k], RFID_input_characters);

don't forget to free later.

The way you had it always you were setting each pointer point to the same address - that is starting address of array RFID_input_characters (the assignment you had there was not copying the string, just setting k-th pointer point to the start of RFID_input_characters array. To copy strings you can use strcpy or its safer versions for instance).

2
Avi Yadav On

Actually char *current_tag_lists[20]; is an array of pointer to chars. In this line current_tag_lists[k]=RFID_input_characters; you are storing the pointer RFID_input_characters in current_tag_lists[k] and not its contents. Since current_tag_lists[k] is pointing to memory address of RFID_input_characters , changing the contents of RFID_input_characters gets reflected in current_tag_lists[k].

You can change your code like this:

  char *current_tag_lists[20];
    while(k<6)
    {
        char RFID_input_characters[12];
        a=0;
        while(a<13){
            if(a==12){
                current_tag_lists[k]=malloc(12);
                strcpy(current_tag_lists[k],RFID_input_characters);
                break;
            }
            else{
                RFID_input_characters[a]='a'; // this example in my code it get value like this
                a++;
           }
         }
        k++;
}

Don't forget to free the memory allocated using malloc or you could end up using all your memory resources.

0
user3629249 On

given the 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'; // this example in my code it get value like this
            a++;
       }
     }
   }

There are several details that need attention:

1) need to initialize the current_tag_lists[] to all NULLs to make it easy when later the malloc'd strings need to be passed to free()

char *current_tag_lists[2] = { NULL };

2) each string needs a unique place to be stored:

char *temp =NULL;
if( NULL == (temp = malloc( 12 ) ) )
{ // then malloc failed
    perror( "malloc failed" );
    cleanup();  // frees all malloc'd areas
    exit( EXIT_FAILURE );
}

// implied else, malloc successful

regarding this line:

while( a < 13 )

the max number of characters per entry is (per the original code and the above mallocf() 12. C references offsets into arrays as 0...(array len -1) So 12 (a<13) would be accessing beyond the upper bound of the array. Leading to undefined behaviour, which can/will lead to a seg fault event.

Suggest the 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[] to 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 ) ) )
    { // then malloc failed
        perror( "malloc failed" );
        cleanup( current_tag_lists );  // frees all malloc'd areas
        exit( EXIT_FAILURE );
    }

    for( int a = 0; a < MAX_RFID_LEN; a++ )
    {
         temp[a]='a'; // this example in my code it get value like this
    } // end for

    current_tag_lists[k]=temp;
    temp = NULL;
} // end for



void cleanup( char *current_tag_lists )
{
    int i;
    for( i = 0; i < MAX_TAG_LISTS; i++)
    {
        // note: ok to pass NULL to free
        free( current_tag_lists[i] );
    }
} // end function: cleanup
4
ikrabbe On

On a microcontroller you may not want to use malloc and your algorithm seems to have very determined needs, as it's normal for microcontrollers. In your algorithm

char *current_tag_lists[20];
while(k<6)
{
    char RFID_input_characters[12];

you define some numbers. First you should use symbols for the constants 20,6 and 12:

enum tag_list_params { tag_list_len=20, rfid_len=12, num_input_cycles=6 };

and replace them in your code.

Now you define the space

typedef char rfid_t[rfid_len];
rfid_t tag_list_space[tag_list_len];

then you can point with current_tag_list into the tag_list_space

char* current_tag_lists[20];

of course you could use the &tag_list_space[a] directly, but you can do

current_tag_lists[k]=(char*)&tag_list_space[k];

to use the variables as you defined them. When you write into the tag list, you can simply point into the space too

char* RFID_input_characters;
for(k=0;k<num_input_cycles;k++)
{
    current_tag_lists[k]=(char*)&tag_list_space[k]; /* save to your target structure */
    RFID_input_characters = (char*)&tag_list_space[k];
    for(a=0; a<rfid_len;++a) {
        RFID_input_characters[a] = 'a';
    }
}

The full test program might look similar to this:

/* enum tag_list_params { tag_list_len=20, rfid_len=12, num_input_cycles=6 }; */
#define tag_list_len 20
#define rfid_len 12
#define num_input_cycles 6

typedef char rfid_t[rfid_len];

char* current_tag_lists[tag_list_len]; /* this is the parameter you want to modify outside 
    you can define it as
        extern char** current_tag_list;
    if you export it in a header file */
static rfid_t tag_list_space[tag_list_len]; /* if you define the space inside of the function,
    the memory will get lost on function exit */

void all_a(void)
{
    int k, a;
    char* RFID_input_characters;
    for(k=0;k<num_input_cycles;k++)
    {
        current_tag_lists[k]=(char*)&tag_list_space[k]; /* save to your target structure */
        RFID_input_characters = (char*)&tag_list_space[k];
        for(a=0; a<rfid_len;++a) {
            RFID_input_characters[a] = 'a';
         }
    }
}

void main(void)
{
    all_a();
}

From gdb output when all_a is ready:

(gdb) p current_tag_lists
$1 = {0x601080 <tag_list_space> 'a' <repeats 72 times>,
    0x60108c <tag_list_space+12> 'a' <repeats 60 times>,
    0x601098 <tag_list_space+24> 'a' <repeats 48 times>,
    0x6010a4 <tag_list_space+36> 'a' <repeats 36 times>,
    0x6010b0 <tag_list_space+48> 'a' <repeats 24 times>,
    0x6010bc <tag_list_space+60> 'a' <repeats 12 times>,
    0x0 <repeats 14 times>}