I want to delete the cell which has "sth" in:
char* a[200];
how should I do it? I tried this but it does not work!
for(i=0;i<100;ti++)
{
if(strcmp(a[i],"sth")!=0)
temp[i]=a[i];
}
a=temp //not sure here
You cannot delete a cell from an array like this. You can set it instead to something arbitrary, like an empty string.
The harder way is:
You may wonder why is a simple thing like this is so complicated. The reason is that the array is a sequence of data in the memory. It works something like a bureau with a lot of drawers. You can tell the program what to put in the drawers, but you can't really get rid only a part of it without destroying the whole bureau. So you have to make a new one.
something like
j=0;
for(i=0;i<100;i++)
{
a[j]=a[i];
if(strcmp(a[i],"sth")) {
j++;
}else{
a[j]=0;
}
}
i didnt free the memory here, since i dont know where the strings came from. If the strings were allocated with malloc they should be freed (if not used elsewhere)
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.