简体   繁体   中英

dynamic char pointers array to strings

in this code I want to get a number of friends and then get the names i want the strings will be allocated dynamically with the lengh of the user input i have used with 2 functions:

void getNum(char** names, int* num)
{
    //somecode
    names = (char*)malloc(*num * sizeof(char));
    //check
}

void getNames(char** names, int* num)
{
    int i = 0;
    int len = 0;
    char name[LEN] = { 0 };

    getchar(); //buffer cleaning
    for (i = 0; i < *num; i++)
    {
        printf("enter #%d friend name: ", i+1);
        myFgets(name, LEN); //getting name and cleaning "\n" at end
        len = strlen(name)+1; // getting the size of string include "/0"
        *(names + i) = (char*)malloc(len * sizeof(char));
        if (*(names[i]) == NULL)
        {
            printf("Error allocating memory!\n"); //print an error message
            return 1; //return with failure
        }
        strncpy(*names, name, len);
    }
}

the second dynamic allocation doens't work for me, overflow eror: "Access violation writing location". If the first allocation will be in the second function it will work fine. Can u explain that? and what I need to do for it will work in that way? thank you in advance...

In function getNames , you used the wrong pointer to check for NULL , names[i] is *(names+i) , not the same as *(names[i]) , also, don't cast malloc 's return value. No need to use sizeof(char) , it's always 1.

*(names + i) = (char*)malloc(len * sizeof(char));
if (*(names[i]) == NULL) // compare to the wrong pointer
{
    printf("Error allocating memory!\n"); //print an error message
    return 1; //return with failure
}
strncpy(*names, name, len); // copy to the wrong buffer

Try the following:

names[i] = malloc(len);
if (names[i] == NULL)
{
    printf("Error allocating memory!\n"); 
    return 1; //return with failure
}
strncpy(names[i], name, len);

Also, in getNum , to allocate an array for char pointers, use

void getNum(char ***names, int *num) {
    *names = malloc(*num * sizeof(char*));
}

You will call it by

char **names;
getNum(&names, &num);

You could also return it by doing char **getNum(...) .

Assuming the first function should allocate an array of pointers, and that the second should allocate individual char arrays to store the individual names, you are lacking an indirection level in first function:

  • you pass it a copy of a char** (C pass parameters by copy)
  • you only use the local copy to store the result of the malloc (which is wrong BTW) and still keep original value in caller and eventually get a memory leak when leaving the function since nothing points to the allocated block any longer

It should be:

char** getNum(int num)   /* no need to pass num by reference */
{
    char **names;
    //somecode
    names = malloc(num * sizeof(char*)); /* do not cast resul of malloc in C */
    //check
    return names
}

And in second function, you should consistenly allocate memory for names[i] (or *(names + i) ), test it for NULL and copy the string there:

    names[i] = malloc(len * sizeof(char));
    if (names[i] == NULL)
    {
        printf("Error allocating memory!\n"); //print an error message
        return 1; //return with failure
    }
    strncpy(names[i], name, len);

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