简体   繁体   中英

How to empty a 2d char array in C?

I am still new with C and I am trying to empty a 2d char array. Here is the declaration:

char arg_array = (char**)calloc(strlen(buf), sizeof (char**));
for(i = 0; i<(strlen(buf)); i++)
{
    arg_array[i] = (char*) calloc (strlen(buf), sizeof(char*));
}

Here is where I try to empty it:

void make_empty(char **arg_array)
{
     int i;
     for(i = 0; i <= BUFSIZ; i++)
     {
        arg_array[i][0] = '\0';
     }
     return;
}

Any help is appreciated

So, am I doing it right because this seems to give me segfaults when I try to add data to the array again and then print it?

Empty is just to have it empty - how can I explain more? lol

There is no need to empty it. Often in C, memory allocation is done with malloc which simply returns to you a block of memory which is deemed owned by the caller. When calloc is called, as well as returning you a block of memory, the memory is guaranteed to be initialized to 0. This means for all intents and purposes it is already 'empty'.

Also I'm not quite sure if your code does what you are intending. Let me explain what it does at the moment:

char arg_array = (char**)calloc(strlen(buf), sizeof (char**));

This line is simply wrong. In C, there is no need to cast pointers returned from calloc because they are of type void * , which is implicitly casted to any other pointer type. In this case, you are storing it in a char type which makes no sense. If you do this:

char ** arg_array = calloc(strlen(buf), sizeof (char**));

Then it allocates an array of pointers of strlen(buf) length. So if buf is "hello" then you have now allocated an array which can store 5 pointers.

for(i = 0; i<(strlen(buf)); i++)
{
  arg_array[i] = calloc (strlen(buf), sizeof(char*));
}

Again, I have removed the redundant cast. What this does is populates the array allocated earlier. Each index of the array now points to a char string of strlen(buf) * sizeof(char *) length. This is probably not what you want.


Your question is more clear to me now. It appears you want to remove the strings after populating them. You can do it two ways:

  • Either free each of the pointers and allocate more space later as you did before
  • Or set the first character of each of the strings to a null character

To free the pointers:

for(i = 0; i<(strlen(buf)); i++)
{
  free(arg_array[i]);
}

To set the first character of each string to a null character:

for(i = 0; i<(strlen(buf)); i++)
{
  arg_array[i][0] = '\0';
}

That is the same code as what you have originally and should be fine.


As proof, the following code will run without errors:

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

int main(void)
{
    char *  buf       = "hello";
    char ** arg_array = calloc(strlen(buf), sizeof (char**));
    unsigned int i;


    for(i = 0; i < strlen(buf); i++) {
        arg_array[i] = calloc(strlen(buf),
                sizeof(char *));
    }

    for(i = 0; i < strlen(buf); i++) {
        arg_array[i][0] = '\0';
    }

    for(i = 0; i < strlen(buf); i++) {
        free(arg_array[i]);
    }


    free(arg_array);
    return EXIT_SUCCESS;
}

If your code is segfaulting, the problem is coming from somewhere else. Did you overwrite the arg_array variable? Are you sure BUFSIZE is equal to strlen(buf) ?

Try this:

void make_empty(char **arg_array, int rows, int cols)
    {
     int i,j;
     for(i = 0; i <rows; i++)
     {
       for(j=0; j<cols;j++)
       {
          arg_array[i][j] = '\0';
       }
     }
    return;
    }

Where rows is number of rows and cols number of cols of your array.

PS This function clears the whole array as you should always do. As I commented before, putting '\\0' as a first char in string does not clear the whole row, it only makes the rest of it ,,invisible'' for functions like printf. Check this link for more information: http://cplusplus.com/reference/clibrary/cstdio/printf/

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