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
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
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)
ti++ should be i++.if (j < 100) a[j] = 0; achieves the same effect and is computationally cheaper, on average. But what is the point? It cannot be a sentinel, since it is not present if j reaches 100. It does not clear all the released elements, since there may be more after a[j].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.