当前位置: 动力学知识库 > 问答 > 编程问答 >

linux - How should work with arrays of string in c

问题描述:

I want to use an array of string the same as this:

char arr[][20] = {"username1", "username2"};

after i have not problem to get values, for example :

printf("%s", arr[0]); // for "username1"

i have problem to insert new string to this array, something like this!? :

arr[2] = "username3"; // or sprintf(arr[2], "%s", "username3");

网友答案:

The wrong way of doing this would be as follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) 
{
    char arr[][20] = {"username1", "username2"};
    printf("%s\n", arr[0]);
    printf("%s\n", arr[1]);
    printf("sizeof arr : %d \n", sizeof(arr)/sizeof(*arr));
    strcpy(arr[2],"username3");
    printf("%s\n", arr[2]);
    printf("sizeof arr : %d \n", sizeof(arr)/sizeof(*arr));
    return 0;
}

because, when you write something like the below

char arr[][20] = {"username1", "username2"};

The first dimension is automatically calculated based on the given initializer, in this case, it is 2 (i.e. index 0 and 1).

Later when you try to access (read or write), the third element i.e. index 2, it is a undefined behavior.

If you want to increase the first dimension of a 2d array, the correct way of doing that would be as follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LEN 20

int main(void) 
{
    char **arr = NULL;
    int n=2, n1;
    int i;

    if ((arr = malloc(sizeof(char *) * n)) == NULL) {
        printf("unable to allocate memory \n");
        return -1;
    }

    for (i=0; i<n ; i++) {
        if ((arr[i] = malloc(sizeof(char) * LEN)) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }
    }

    strcpy(arr[0], "username1");
    strcpy(arr[1], "username2");

    printf("%s\n", arr[0]);
    printf("%s\n", arr[1]);

    /* add 5 more elements to arr */
    n1 = 5;

    if ((arr = realloc(arr, sizeof(char *) * (n + n1))) == NULL) {
        printf("unable to allocate memory \n");
        return -1;
    }

    for (i=n; i<n1+n ; i++) {
        if ((arr[i] = malloc(sizeof(char) * LEN)) == NULL) {
            printf("unable to allocate memory \n");
            return -1;
        }
    }

    strcpy(arr[2], "username2");
    strcpy(arr[3], "username3");
    strcpy(arr[4], "username4");
    strcpy(arr[5], "username5");
    strcpy(arr[6], "username6");

    /* if you uncomment the below, it will lead to undefined behavior */
    //strcpy(arr[7], "username7");

    for (i=0; i<n+n1 ; i++)
        printf("%d : %s \n", i, arr[i]);

    for (i=0; i<n+n1 ; i++) 
        free(arr[i]);
    free(arr);

    return 0;
}

$ ./a.out 
username1
username2
0 : username1 
1 : username2 
2 : username2 
3 : username3 
4 : username4 
5 : username5 
6 : username6 
$ 

Hope this helps!

网友答案:

You're trying to insert a char* into an array of char[20]. That won't work. You could use strcpy as

strcpy(arr[2], "username3");

if you had allocated sufficient space for a third string, which you haven't. I suggest you read up on malloc and friends; you can't just append a string to this array.

网友答案:

With this static initialization, arr will not dynamically resize to hold a new entry (pointers to entries in the stack generally don't like being written to).

In order for this to work, arr should be of char ** and allocated properly:

// allocate enough for three entries in arr
char **arr = (char **) malloc(3 * sizeof(char *));
网友答案:

C does not have dynamically growing data structures built-in. If you want an array that can hold an increasing number of elements, you need to involve dynamic memory allocation. This is usually done using the realloc() function, which can be used to allocate and then re-allocate a memory block, so that you can grow the allocation as needed.

分享给朋友:
您可能感兴趣的文章:
随机阅读: