Reputation: 553
I have two multi dimensional char arrays. They may have duplicates. I want to clear the duplicates in the second one. will assigning the particular element in the second array to NULL, clears it or should I assign it to a "/0".
for(i=0; i<10; i++){
for(j=0; j<10; j++){
if(!strcmp(a[x][i], b[x][j])){
b[x][j]=NULL;
}
i++;
}
Please give me your inputs.
Upvotes: 0
Views: 485
Reputation: 6258
That really depends on a lot of things.
Are the strings malloc
'ed? If they are you should probably free
them and set the pointer to NULL. And then when you pass the cleaned up array, you need to check if the string is NULL
, before you do what ever you need to do with it.
If the strings are static, or if you don't want to free them, because they are used else where, then you can set them to either NULL
or '\0'
. If you choose the later, then you should check for strlen(s) == 0
or if s[0] == '\0'
.
The thing is, you can do either, it probably doesn't mean much which you choose.
Edit
I'll clarify a bit.
What you need to do depends on whether you have an of arrays of char
's (which is '\0' terminated) or an array of pointers to strings.
In the first case, if you want to "remove" a string, you can either change all the character in the array to '\0', or just the first. And use strlen
or `s[0] == '\0' to determine if the string is empty.
In the second case, you should free the pointer, and set it to NULL
. To check if the string is "empty", test for NULL
.
The difference lies in the relationship between pointers and arrays in C, which is not trivial, see here.
Upvotes: 1