c - Warning: comparison with string literals results in unspecified behaviour

c – Warning: comparison with string literals results in unspecified behaviour

c – Warning: comparison with string literals results in unspecified behaviour

You want to use strcmp() == 0 to compare strings instead of a simple ==, which will just compare if the pointers are the same (which they wont be in this case).

args[i] is a pointer to a string (a pointer to an array of chars null terminated), as is & or <.

The expression argc[i] == & checks if the two pointers are the same (point to the same memory location).

The expression strcmp( argc[i], &) == 0 will check if the contents of the two strings are the same.

There is a distinction between a and a:

  • a means the value of the character a.
  • a means the address of the memory location where the string a is stored (which will generally be in the data section of your programs memory space). At that memory location, you will have two bytes — the character a and the null terminator for the string.

c – Warning: comparison with string literals results in unspecified behaviour

You cant compare strings with == in C. For C, strings are just (zero-terminated) arrays, so you need to use string functions to compare them. See the man page for strcmp() and strncmp().

If you want to compare a character you need to compare to a character, not a string. a is the string a, which occupies two bytes (the a and the terminating null byte), while the character a is represented by a in C.

Related posts on C++ Warning  :

Leave a Reply

Your email address will not be published.