Cannot compare pointers to strings?

Click For Summary
SUMMARY

The discussion centers on the issue of comparing a pointer to a string in C++, specifically highlighting a compilation error encountered with GCC. The error arises from attempting to compare an element of an unsigned char array to a string literal, which is a pointer type. The correct approach is to use single quotes for character comparison, as in ' ', instead of double quotes, which denote string literals. This distinction is crucial for adhering to ISO C++ standards.

PREREQUISITES
  • Understanding of C++ data types, specifically char and unsigned char
  • Familiarity with array handling in C++
  • Knowledge of pointer types and string literals in C++
  • Basic understanding of GCC compiler error messages
NEXT STEPS
  • Review C++ data types and their differences, focusing on char vs. string
  • Learn about array references and how they interact with functions in C++
  • Study the differences between single quotes and double quotes in C++
  • Explore common GCC compiler errors and their resolutions
USEFUL FOR

C++ developers, programming students, and anyone looking to deepen their understanding of type comparisons and array handling in C++.

dE_logics
Messages
742
Reaction score
0
After compiling the code below, GCC says that you can't compare a pointer to string by iso C++ standards.

But I don't see a pointer anywhere (I don't know pointers for now...did it long ago, now forgot)...may be it has to do with the fact that arrays are called by reference.

Code:
char convert(unsigned char input[]);
main()
{
	unsigned char input[1000];
	convert(input);
}
char convert (unsigned char input[])
{
	int i;
	if(input[0] == " ");
}
 
Technology news on Phys.org
dE_logics said:
Code:
if(input[0] == " ");
input[0] has type unsigned char&.
" " has type const char*.

You meant ' ' which has type char.
 
Oh...damn...I meant single quotes. :P
 

Similar threads

Replies
5
Views
2K
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
14
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 65 ·
3
Replies
65
Views
8K
  • · Replies 3 ·
Replies
3
Views
3K