1

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
6
  • Not sure what you are trying to achieve. Do you want to delete a single element from the array, because that is not that simple, or do you want something else? Commented Aug 10, 2012 at 9:44
  • delete single element from array. Commented Aug 10, 2012 at 9:45
  • 2
    By "delete", do you mean freeing the memory it points to, make it point to the next string, set it to NULL, or some combination of these? Commented Aug 10, 2012 at 9:46
  • make it point to the next string. sorry if I didnot ask well Commented Aug 10, 2012 at 9:47
  • maybe your coming to C from another language? I think what you want is a list, hash-table or dynamic array of some description. These aren't built into C as standard but you can get 3rd party implementations to avoid reinventing the wheel, e.g. uthash.sourceforge.net/utarray.html Commented Aug 10, 2012 at 9:55

2 Answers 2

2

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Setting a[j] to zero has no lasting effect except in the last iteration, because a[j] is overwritten in the next iteration. If your intent was to set all the trailing elements of a to zero after the kept elements had been moved down, then a better way is to set them to zero in a second loop after this one that increments j from its final value from the first loop to 100. Also, there is a typo in the first loop: ti++ should be i++.
i added the a[j] to treat the last element without adding an if statement at the end of the code. Otherwise if the last element is "sth" it will be kept
Adding 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].
2

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:

  • count the items you want to delete
  • create a new smaller array
  • copy the items you need from the old array to the new one
  • delete the old one.

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.

1 Comment

Alternatively, in this case you can set an "empty" pointer to point at NULL, rather than the string. The program would then have to check every pointer against NULL before trying to access the data pointed at.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.