C STRINGS
Re:C STRINGS
Slacker, perhaps you should brush up on your basic C programming. See this thread for information on C strings, and this one for an explanation of the bitwise operators (for your earlier question about the ketboard driver).
Re:C STRINGS
ok i got the string workin and i even get the string to print but for some reason when i try to compare the string to another, it doesnt match up...
void go_exe(char *command)
{
if(command=="clrscr")
{
clear();
printPROMPT();
};
};
void go_exe(char *command)
{
if(command=="clrscr")
{
clear();
printPROMPT();
};
};
Re:C STRINGS
Slacker -- get a clue about C programming! You've just made the mistake everyone makes once, in their first week of programming C.
Use strcmp/stricmp.
Use strcmp/stricmp.
Re:C STRINGS
If you think about how the comparison works, and just what you are comparing here, you'll see two reasons this won't work.slacker wrote: ok i got the string workin and i even get the string to print but for some reason when i try to compare the string to another, it doesnt match up...
void go_exe(char *command)
{
if(command=="clrscr")
{
clear();
printPROMPT();
};
};
In the code you have above, [tt]command[/tt] is a char pointer, whereas "clrscr" is a six element char array constant whose values are
[tt]clrscr[[0]] = 'c'
clrscr[1] = 'l'
clrscr[2] = 's'
clrscr[3] = 'c'
clrscr[4] = 'r'
clrscr[5] = '\00'
[/tt]
You are, in effect, comparing a 4-byte address to the first four elements of the array. Needless to say, this isn't going to work; im surprised the compiler didn't give you an implicit type-cast warning. You need to use the indirection operator, '*', in front of [tt]command[/tt] to get the object it points to rather than the value of the pointer.
However, even if you had used [tt]*command[/tt], it still wouldn't have worked as you wanted it to, because the equality operator ("==") only compares two simple scalar values of the same size; in the case of an array, only the currently indexed variables are compared, with zero (the first item in the array) being the default. So if you have
Code: Select all
char foo[] = "Hello, World!";
char bar[] = "Hello, Sucker!";
Code: Select all
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <string.h>
int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2)
{
if (*s1 == 0)
return 0;
s1++;
s2++;
}
return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
}
EDIT: Fixed the zero-susbcripted arrays (I forgot that in BBcode, you need to use "[[[[" and "]]]]" for text outside of a code block.
Re:C STRINGS
that DJGPP compare string didnt work....
i made one that does work.
int cmpstr(const char *s1, const char *s2)
{
int i=0;
int equal=1;
char *temp;
if(s1[0]=='\00')
equal=0;
if(s2[0]=='\00')
equal=0;
while(*s1!='\00')
{
if(*s1!=*s2)
equal=0;
*s1++;
*s2++;
};
if(*s2!='\00')
equal=0;
if(equal==0)
return 0;
if(equal==1)
return 1;
};
i made one that does work.
int cmpstr(const char *s1, const char *s2)
{
int i=0;
int equal=1;
char *temp;
if(s1[0]=='\00')
equal=0;
if(s2[0]=='\00')
equal=0;
while(*s1!='\00')
{
if(*s1!=*s2)
equal=0;
*s1++;
*s2++;
};
if(*s2!='\00')
equal=0;
if(equal==0)
return 0;
if(equal==1)
return 1;
};
Re:C STRINGS
I surprised that the strcmp() code didn't work. What was the problem? Was it a compiler error, or a behavior error?
It should be pointed out that the return values for strcmp() are not a straight TRUE or FALSE. Rather, it returns a negative number if s1 would sort lexigraphically before s2, zero if they are identical, and a positive number if s1 sorts after s2. So,
I had forgotten to mention this, which I should have, as it is rather confusing if you aren't aware of it.
It should be pointed out that the return values for strcmp() are not a straight TRUE or FALSE. Rather, it returns a negative number if s1 would sort lexigraphically before s2, zero if they are identical, and a positive number if s1 sorts after s2. So,
Code: Select all
strcmp("cat", "clrscr") == -11
strcmp("clrscr", clrscr") == 0
strcmp("clrscr", "at") = 2