How to clean the Array after write some information

I first declare a pointer array

int q,k;
char ***schArray;
schArray = malloc(sizeof(char**)*1000);
    for(q = 0; q < 1000; q++) {
        schArray[q] = malloc(sizeof(char*)*100);
        for(k = 0; k < 1000; k++) {
            schArray[q][k] = malloc(sizeof(char)*100);
        }
    }

char buf[80]={0};

Then I read some data from pipe which put it in the buf and copy it to the pointer Array

strcpy(schArray[commandnum][0], buf); 

After that, I want to clean the content of Pointer Array. I use the following code.

schArray[commandnum][0]=NULL;

After that, I cannot write any data to schArray[commandnum][0] by the code

strcpy(schArray[commandnum][0], buf);

Is there any mistake in the code above?

Answers


I think you want to set the first character of an allocated string to '\0'. You'd do this using

schArray[commandnum][0][0] = '\0';

Your current code replaces (and leaks) the pointer to the buffer allocated by malloc(sizeof(char)*100)

As an aside, and before anyone pulls me up on it, note that sizeof(char) is guaranteed to be 1 so the malloc can be simplified to malloc(100).


Need Your Help

EF6 include with join not including.

entity-framework

I'm well aware this is an issue with a clear solution in more simple situations,

Should I install GRUB bootloader?

ubuntu boot grub

I have installed an industrial computer with Ubuntu Server 14.04.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.