tmsbrndz
tmsbrndz

Reputation: 1347

Iterating through string array

i have array of strings.

char strings[][30]={"word1","word2",......,""}; 

Iterated over them but at the end i got message Segmentation fault (core dumped)

Tried with following code :

for(int i=0;strings[i] != "";i++){printing items...}

I think the problem is with != "" but why isn't it working?

Upvotes: 3

Views: 1852

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

In this array initializer

char strings[][30]={"word1","word2",......,""}; 

the last element of the array in fact is initialized the following way

char strings[][30]={"word1","word2",......, { '\0' ) };

In this condition

strings[i] != "";

there are compared two pointers: the first one is pointer to the first character of the character array string[i] and the second one is pointer to the first character of the string literal "".

For clarity the condition can be equivalently rewritten the following way

&strings[i][0] != &""[0];

It is evident that the pointers are unequal each other because the character array occupies its own extent of memory. The array does not overlap the static memory occupied by the string literal.

To make the loop correct you can rewrite the condition the following way

for(int i=0; *strings[i]; i++){printing items...}

or

for(int i=0; strings[i][0] != '\0' ;i++){printing items...}

Take into account that if you'll even write for example the following condition

"" == ""

it is not necessary it will yield true because depending on compiler options the compiler can store these two string literals separately and not as one string literal. In this case the addresses of the string literals will be different.

Upvotes: 0

Stephan Lechner
Stephan Lechner

Reputation: 35154

The contents of character arrays cannot be compared with == or != in C. Both string[i] and "" would decay to a pointer, and the respective pointer values will never be equal. Hence, you exceed array bounds and yield undefined behaviour then.

But you could easily check if the first character of a string is the string terminating character or not:

char strings[][30]={"word1","word2",""};

for(int i=0;strings[i][0] != '\0';i++)
{ ...
}

Upvotes: 0

dbush
dbush

Reputation: 223699

You can't compare strings that way. Arrays in most contexts decay to a pointer to the first element, and strings are arrays of characters. What you're actually doing is comparing the address of strings[i] with the address of the string literal "". These are always unequal, so the comparison will always be true.

As a result, you iterate past the end of the array. Reading past the end of an array invokes undefined behavior, which in this case manifests as a crash.

You need to use strcmp to compare strings. It returns 0 if the strings match.

for(int i=0;strcmp(strings[i],"") != 0;i++)

Upvotes: 2

user2736738
user2736738

Reputation: 30926

Use strcmp to compare strings in C. == or != doesn't work for strings in C.

Upvotes: 2

Related Questions