C STRINGS

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
slacker

C STRINGS

Post by slacker »

ok i got my keyboard workin...
in C..how would i declare a string and how would i add characters one by one to that string..
thank you.
Curufir

Re:C STRINGS

Post by Curufir »

You cannot be serious....
Schol-R-LEA

Re:C STRINGS

Post by Schol-R-LEA »

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).
slacker

Re:C STRINGS

Post by slacker »

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();
};

};
Tim

Re:C STRINGS

Post by Tim »

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.
Schol-R-LEA

Re:C STRINGS

Post by Schol-R-LEA »

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();
};

};
If you think about how the comparison works, and just what you are comparing here, you'll see two reasons this won't work.

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!";
Then the test [tt]if (foo == bar)[/tt] returns true, because foo[[0]] and bar[[0]] are both 'H'. Similarly, if you were to call your function with the argument "cat", the expression [tt]if (*command == "clrscr")[/tt] will evaluate to true, since only the first elements of the two strings get compared. To compare two strings, you'd need to loop through them, and compare each element in turn:

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);
}
Fortunately, as Tim pointed out earlier, strcmp() is a standard library function; indeed, this version is taken straight from the DJGPP library code. Hope this clarifies the issue somewhat.

EDIT: Fixed the zero-susbcripted arrays (I forgot that in BBcode, you need to use "[[[[" and "]]]]" for text outside of a code block.
slacker

Re:C STRINGS

Post by slacker »

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;

};
Schol-R-LEA

Re:C STRINGS

Post by Schol-R-LEA »

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,

Code: Select all

strcmp("cat", "clrscr") == -11
strcmp("clrscr", clrscr") == 0
strcmp("clrscr", "at") = 2
I had forgotten to mention this, which I should have, as it is rather confusing if you aren't aware of it.
slacker

Re:C STRINGS

Post by slacker »

yea i didnt know about the negative/positive numbers...
Post Reply