Warning : '<' signed/unsigned incompatibility error

Hi all!

I'm really brand new to C++ and I'm currently learning it for fun. I'm working on prototype functions, and I got an error I can't figure out!

I get an error saying “warning : '<' : signed/unsigned incompatibility” but I haven't defined an unsigned int anywhere. Actually, I don't even really know much about them except that they allow bytes from -127 to 127 instead of 0 to 256 for signed int's if I understood correctly...

The function itself is supposed to make a pointer go to the end of a string and add the Null Byte at the end. There's probably an already-existing way to do so, but I don't know it. Plus it is fun making this.

In the below code I'll be making comments for most lines so you may correct me if I'm not doing what I think I am.
Anyway, here's the code of my "my_fct.h" header file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>   //Includes basic functions necessary in C as 'scanf' or 'printf'.
#include <string.h>  //Includes string functions such as 'strcpy' or 'strcmp'.
#include <iostream>  //Not sure about what this does.

int strend(char[]);  /* Specifies the existence of a function 'strend' that uses a char
                      * string as argument and that returns a value of integer type. */

char string[];       //Declares a global string called 'string'.


int strend(char string[])  /* Declares a function that uses the global string 'string'
                            * as argument. */
{
	int i;
	char *pointer=string; //Declares a pointer '*pointer' that points to 'string'.

	for (i=0 ; i<strlen(string) ; i++) /* Repeat as many times as the number of
                                            * characters of 'string'. */
		*pointer++; //'Pointer' points to the character next to the one it was pointing.

	*pointer='\0';  //Adds the null byte after the 'for' loop is ended.

	return 0;  //If the function was successful, return the value '0'.
}
Last edited on
strlen return size_type, which is unsigned int. In line 17 you compare int i with strlen(string).

Warnings are not errors. In most cases you can just ignore them.
Last edited on
> In most cases you can just ignore them.

In no case should you ignore a diagnostic from the compiler.
Examine it carefully, and then you may decide that it is safe to let it be.

In this particular case, the signed value would be interpreted as an unsigned value for the comparison, and it should be ok.
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    std::cout << std::boolalpha << ( -12 < 10  ) << '\n'  // true
                                << ( -12 < 10U  ) << '\n' ; // false
}


Last edited on
Thanks to both of you! ☺
Topic archived. No new replies allowed.