Cannot compare pointers to strings?

In summary, "Cannot compare pointers to strings" is an error message that occurs when trying to compare two pointers that are pointing to strings. This is not allowed because pointers are used to store memory addresses while strings are used to store a sequence of characters. Instead, you can use string comparison functions to compare the actual strings or use other data types for comparison. This error is specific to languages that use pointers, while other languages may allow comparison between pointers and strings.
  • #1
dE_logics
742
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
  • #2
dE_logics said:
Code:
if(input[0] == " ");
input[0] has type unsigned char&.
" " has type const char*.

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

1. What does "Cannot compare pointers to strings" mean?

"Cannot compare pointers to strings" is an error message that occurs when trying to compare two pointers that are pointing to strings. This means that the comparison is not allowed because the pointers are not pointing to the same location in memory.

2. Why is it not allowed to compare pointers to strings?

Comparing pointers to strings is not allowed because pointers are used to store memory addresses, while strings are used to store a sequence of characters. Therefore, comparing the two would not provide a meaningful comparison and could result in unexpected behavior.

3. How can I fix the "Cannot compare pointers to strings" error?

To fix this error, you can use string comparison functions such as strcmp() or strncmp() to compare the actual strings instead of their pointers. These functions will return an integer value based on the comparison of the strings, which can then be used in conditional statements.

4. Can I compare pointers to strings in other programming languages?

The "Cannot compare pointers to strings" error is specific to programming languages that use pointers, such as C and C++. Other languages may use different data types for strings and therefore allow comparison between pointers and strings without an error.

5. Are there any alternatives to comparing pointers to strings?

Yes, instead of comparing pointers to strings, you can compare the actual strings themselves using string comparison functions. You can also use other data types, such as integers or characters, to store and compare data rather than using pointers.

Similar threads

  • Programming and Computer Science
Replies
5
Views
846
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
2
Replies
65
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top