something about "for(stuff;first[i] && second[i];stuff)" (go to bottom threeish posts)

the problem is that when i run it it does not trigger the 'if'. I give the debugger input parameters to be "/v /n /o" and it does not show that any of them were triggered...

as far as I THINK ArgumentInput as an array of char arrays correct? in c# the value's of vars are compared, is there a different comparison in c++?

is the format of ArgumentInput[i] something other than a char[]? and if it is, how can I convert it to one?

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
int main (int NumberOfArguments, char *ArgumentInputs[])
{
    char *ValueToConvert;
    char *OriginalBase;
    char *NewBase;
    char v[] = {'/','v','\0'};
    char V[] = {'/','V','\0'};
    char o[] = {'/','o','\0'};
    char O[] = {'/','O','\0'};
    char n[] = {'/','n','\0'};
    char N[] = {'/','N','\0'};
    for (int i=1; i < NumberOfArguments; ++i)
    {
        if (ArgumentInputs[i] == v || ArgumentInputs[i] == V)
        {
            cout << ArgumentInputs[i] << " found (v) \n";
        }
        if (ArgumentInputs[i] == o || ArgumentInputs[i] == O)
        {
            cout << ArgumentInputs[i] << " found (o) \n";
        }
        if (ArgumentInputs[i] == n || ArgumentInputs[i] == N)
        {
            cout << ArgumentInputs[i] << " found (n) \n";
        }
    }
    cin.get();
    return 0;
};
Last edited on
You can't compare two char* using == if you want to compare the contents of the C-strings. Use strcmp() instead.
Indeed. Now you're just comparing their addresses. Besides strcmp, you could use std::string.
Last edited on
how do i override the default operation of the '==' sign? I make a struct with the name *char, use some inheretance with the other ^char and override their "==" operator with my "==" operator?
Last edited on
A pointer to a character is a built in type, so that is impossible. Is there some reason you absolutely can't use strcmp() or std::string?
there is not a reason I can't, but i want to get some practice in those sort of things...
so then i shall attempt to create a char* compare myself... Thanks for the help... I might need more help in the end because of my stubbornness... Thanks you for your help... now I know that is compares the location rather than the values... big difference...
Thanks for the help everyone... I created a simple *char compare function... if you are interested the code is here... and it is commented : )

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
43
44
45
46
47
48
static bool CharArrayEqual(char* First, char* Second)
{
    //FirstLength represents the length of the *char First
    int FirstLength;
    //more represents a bool stating if there is more to the array
    bool more = true;
    //The for loop iterates until 'more' is false
    //i represents the number of chars in the *char First (including the '\0'
    for (int i = 1; more; ++i)
    {
        //If the char in the *char at the 'i'th element is a '\0' then more is set to false
        //Else the for iterates again checking the next element for a '\0'
        if (First[i-1]=='\0')
        {
            //Set more to false because there is not more to the *char First
            more = false;
            //Set FirstLength to i, which contains the int representing the position of the '\0')
            FirstLength = i;
        }
    }
    //Implement same process as above for *char Second
    int SecondLength;
    more = true;
    for (int i = 1; more; ++i)
    {
        if (Second[i-1]=='\0')
        {
            more = false;
            SecondLength = i;
        }
    }
    //If lengths are different, then value of First is not equal to value of Second
    if (FirstLength!=SecondLength)
    {
        return false;
    }
    //If the lengths are the same then compare every char in First and Second
    //If the two char are different then they are not equivalent
    for (int i=0; i < FirstLength; ++i)
    {
        if (First[i]!=Second[i])
        {
            return false;
        }
    }
    //If the for loop iteration finishes without returning false then the values of First and Second are equal and return true
    return true;
}
This can be done a lot simpler. With a single loop. Should I show, or do you want to try to figure it out yourself?
I want to try, cause I think i know what you are getting at...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
static bool CharArrayEqual(char* First, char* Second)
{
    for (int i = 0;;++i)
    {
        if (First[i]!=Second[i])
        {
            return false;
        }
        if (First[i]=='\0')
        {
            return true;
        )
    >
>


thanks for making me think : )
Last edited on
That's almost it. One thing you have to fix is that you only check when first ends. What if second is shorter?
I myself would write this as
1
2
3
for( int i = 0; first[i] && second[i]; i++ )
   if( first[i] != second[i] ) return false;
return true;
Not that it makes any difference. Just wanted to point out a more compact way to write it.
the order of the 'if's removes that problem. since the first string is checked against the second string before they are checked for a "\0" if one terminates and the other doesn't then it will return false.

my question about yours is how does it know when the *char terminates? is that the first[i] && second[i] portion? and what do those check?
also note that the function should be taking const char*s and not char*s:

1
2
3
4
5
6
7
8
bool CharArrayEqual(const char* a,const char* b)
{
  for(; *a || *b; ++a, ++b)
  {
    if(*a != *b)  return false;
  }
  return true;
}


But of course, no need to reinvent the wheel here. strcmp already does this:

1
2
3
4
bool CharArrayEqual(const char* a,const char* b)
{
  return !strcmp(a,b);
}


EDIT: fixed the above..

my question about yours is how does it know when the *char terminates?


c-strings are terminated with a null character (the char == 0).
Last edited on
reinventing the wheel is fun... and i know what the default terminator is... but how does his code specifically know that there was a "\0"?

and what did the first[i] && second [i] mean?
Last edited on
'\0' is represented by, as Disch stated, the ASCII value 0. So the following are all equal:
1
2
3
a[i] == '\0'
a[i] == 0
!(a[i])
is that the first[i] && second[i] portion? and what do those check?
Integers (int, short, char, etc.) can be converted to booleans. 0 evaluates to false and everything else evaluates to true. if(x) is equivalent to if(x!=0)
+1 to Zhuge and hamsterman

on a side topic:

'\0' is not really a special character like many seem to think. A backslash (\) followed by numerical digits merely provide a way to input literal numerical values into a string (in octal).

The character '\0' has a literal value of 0. Therefore the below two lines are exactly the same:
1
2
char null = '\0';
char null = 0;


So '\0' is equal to 0, just like '\5' is equal to 5.
Thanks for the help everyone! To recap...

I learned that locations of memory are compared instead of the values of the arrays

I Learned that an int and a bool are directly convertable

I learned that you can set a ascII char using ints

Bye everyone.
Topic archived. No new replies allowed.