warning: comparison between signed and unsigned integer expressions

May 16, 2008 at 12:23pm
Hi folks,

This is my first post so I'd like to say 'Hi' to you all. :)

I've arrived here because I'm having problems with my script and was wondering if you could help me?

I have a linux .cpp file that has C++ code in it. Someone has been helping me write the code as I'm not very good with it. But they're having some personal troubles now and don't have the time to help me now.

This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void eCbpMain::loadEmuList()
{
	char emuactive[100];
	emuactive[0]=0;

	emulistbox->beginAtomic(); 
	emulistbox->clearList();
	FILE *fp = fopen("/var/bin/emuactive","r");
	if (fp) {
		memset(emuactive, 0, sizeof(emuactive));
		fgets(emuactive, 200, fp);
		fclose(fp);
		int a=0;
		for (a=0; a<strlen(emuactive); a++) if (emuactive[a]==(char)0x0a) emuactive[a]=0;
	}

	if(eSystemInfo::getInstance()->getHwType() ==  eSystemInfo::DM500)
	{	new eListBoxEntryText(emulistbox, "None", (void*)0, 2);
	} else
		new eListBoxEntryText(emulistbox, "Common Interface", (void*)0, 2);
	emulist[0]="None";
	eListBoxEntryText *lblast, *lbselected=0;

	DIR *d = opendir("/var/script");
	while (struct dirent *e=readdir(d))
	{
		if ( strstr(e->d_name,"_em.sh")) 
		{
			memset(emuname, 0, sizeof(emuname));
			strncpy(emuname, e->d_name, strlen(e->d_name)-6);
			lblast = new eListBoxEntryText(emulistbox, readEmuName(emuname), (void*)emulistbox->getCount(), 2);
			emulist[(int)emulistbox->getCount() - 1]=emuname;
			if ( strcmp(emuactive, emuname)==0 ) lbselected=lblast;
		}
	}
	closedir(d);
	if (emulistbox->getCount())
		emulistbox->sort();	
	if (lbselected) emulistbox->setCurrent( lbselected, false );
	emulistbox->endAtomic();
	
}


This is the line that I've having problems with...

for (a=0; a<strlen(emuactive); a++) if (emuactive[a]==(char)0x0a) emuactive[a]=0;

This is the error I get...

test.cpp: In member function `void eCbpMain::loadEmuList()':
test.cpp:56: warning: comparison between signed and unsigned integer expressions


Any help you can offer would be greatly appreciated.

Thanks
-Devilfish
May 16, 2008 at 12:39pm
The compiler is warning you that the comparison a<strlen(emuactive) is comparing a signed integer (a) with an unsigned integer (strlen returns a size_t, which is an unsigned integer).
To remove the warning change int a=0; to unsigned int a=0;
Note that this is one of the circumstances where choice of code formatting has made the problem slightly harder for you.
if you had
1
2
3
4
5
6
7
for (a=0; a<strlen(emuactive); a++) 
{
    if (emuactive[a]==(char)0x0a) 
    {    
        emuactive[a]=0;
    }
}

then the compiler would have told you the problem was in
 
for (a=0; a<strlen(emuactive); a++) 


If you do find a problem in a line like this it is always worth doing a quick reformat to see if you can narrow the problem down (you can always edit it back afterwards when fixed:-)
May 16, 2008 at 12:46pm
Wow...I can't believe how fast that was! :)

That's probably a very beginner question for you guys lol but thank you because it's got me past that error. :)

I have another error after that but I will try to debug it myself before I burden you guys again.

Thanks very much for the above reply, excellent! :)
Topic archived. No new replies allowed.