简体   繁体   中英

Assigning 2D string arrays using char *

I am trying to assign a 2D array of strings in the following way:

char *text_data[10][4]; //10 rows, 4 columns

//All rows need to be same
for (i = 0; i < 10; i++)
{
    strcpy(text_data[i][0], "a");
    strcpy(text_data[i][1], "xyz");
    strcpy(text_data[i][2], "b");
    strcpy(text_data[i][3], "xyz");  
}

However, this doesnt work. What am I doing wrong?

char *text_data[10][4]; is matrix of pointer, each location can point a string.

but you do not initialize or allocate memory so you can't call string copy.

You can do like:

 text_data[i][0] = "a";
 text_data[i][1] = "xyz";

Second: I also feel you wanted to declare text_data as:

char text_data[10][4];

because every string is of length 3 or less, then do like:

strcpy(text_data[0], "a");
strcpy(text_data[1], "xyz");
strcpy(text_data[2], "b");
strcpy(text_data[3], "xyz");  

No need of loop

strcpy will only copy to a preallocated buffer so try this

char text_data[10][4][4];

If you have a double array of String you essentially have a triple array because a string is an array of char.

I'm pretty sure that your code should look like this, no restrictions on string length. No 3D arrays of chars. But I'm rather new to programming and can be wrong, if so-correct me.

#include <stdio.h>


int main(void)
{
    char *text_data[10][4]; //10 rows, 4 columns
    int i;
//All rows need to be same
    for (i = 0; i < 10; i++)
    {
        text_data[i][0]= "a";
        text_data[i][1]="xyz";
        text_data[i][2]= "b";
        text_data[i][3]= "xyz";  
    }

    for (i = 0; i < 10; i++)
    {
        puts(text_data[i][0]);
        puts(text_data[i][1]);
        puts(text_data[i][2]);
        puts(text_data[i][3]);
    }
return 0;
}

Hopefully, I understand what your code is trying to do. Have re-written it as:

    #include <stdio.h>
    void main()
    {
        // allocate 40 uninitialized pointers to char strings
    char *text_ptr[10][4]; //10 rows, 4 columns

        int i;

    for (i = 0; i < 10; i++)
    {
    // essentially, copy ptr to string literal into an array element
        text_ptr[i][0] = "a";
        text_ptr[i][1] = "xyz";
        text_ptr[i][2] = "b";
        text_ptr[i][3] = "xyz";

        // print two elements from each row for a quick check
        printf("\n%s, %s", text_ptr[i][0], text_ptr[i][1]); fflush(stdout);

      }

     } // end of main;
     // Note.  This code WAS tested in an Eclipse/Microsoft C compiler environment.

strcpy() depends on an area for the string to be allocated, so that it can copied into. In the above code there is an array of char pointers, each of them is set to the address of the literal that is being assigned.

In this code you cannot update the assigned strings. If you want to modify the strings then you will need to do a malloc() for each of the 40 ([10][4]) pointers, and each malloc() needs to be for the length (or max length) of the string to be assigni.e. associated with that pointer element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM