0

My .c programme gives an object a 'name' when its created. I need to be able to excute different tasks depending on that name. I very new to this and have tried a couple of ways with no success. Here is what I had come up with..

if (name ==  "james"){

    //Do a bunch of stuff
    } 
if (name ==  "tom"){

    //Do a bunch of stuff
    } 

This was not successful. Is there a way so that if the 'name' is one thing it wont execute the others?

Thankyou so much for any help

2
  • 3
    Also please don't tag C questions as C++, they are completely different languages. Commented Apr 20, 2012 at 10:13
  • Now it was tagged as C, not it's tagged as C++ again, what's going on? He said in the beginning that it's a ".c programme", so I'd assume he meant C.. Commented Apr 20, 2012 at 10:27

5 Answers 5

3

if 'name' is a C++ string, what you have written should work fine. If it's a char[] or char *, use strcmp.

Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou for the answer, how would i use this with the above code?
If name isn't a C++ string, it should be.
2

Have a look at the library function strcmp. When you use == you will just compare the pointers itself, not the actual string array lying behind them.

4 Comments

Not if name is a string, as he claims.
Well, it's quite usual to call char* or char[] "strings" in C, because "pointer to char" or "char array" just haven't the same feel.
Yes, but what is quite usual in C is sometimes very unusual in C++. When talking about C++, "string" normally means std::string, and '\0' terminated char* or char[] will be usually be called "C-style strings" if the context isn't totally unambiguous.
Yes, I know, but the question was about C (which was kind of obvious, because if name were an actual std:string, his code were correct).
1

In C, a string is defined as a sequence of characters which are terminated with \0. A string constant is normally represented within `", for example, char a[10] = "hello".

In order to compare two strings, you can use library functions like strcmp() which is available in string.h. Do man strcmp to read more about this function.

#include <stdio.h>
#include <string.h>

int main()
{
        char name[] = "tom";

        if (strcmp(name, "tom") == 0) {
            printf("name is tom! \n");
        } else if (strcmp(name, "bob") == 0) {
            printf("name is bob! \n");
        } else {
            printf("who is this?! \n");
        }

        return 0;
}

The strcmp could have been implemented in the following ways (array and pointer versions for your reference)

int strcmp1(char a[], char b[])
{
        int i=0;
        while (a[i] == b[i]) {
                if (a[i] == '\0')
                        return 0;
                i++;
        }

        return a[i]-b[i];
}

int strcmp2(char *a, char *b)
{
        while (*a == *b) {
                if (*a == '\0')
                        return 0;
                a++; b++;
        }
        return *a-*b;
}

1 Comment

Just a note: These implementations are both completely the same (except the use of an index of course), because you cannot pass actual arrays as function parameters; they are actually pointers as well.
1

You need to use strcmp as:

if (!strcmp(name,"james")){

Using == compares the contents of name(starting address of the string) with the starting address of the string literal "james" which is certainly now what you want.

3 Comments

strcmp doesn't work with C++ strings. His problem is that he's not declared name with the correct type; if name is std::string, his code should work.
@JamesKanze: The question clearly talks about a .c file, so he is talking about C not C++.
The question was clearly tagged C++ when I looked at it. I assumed that this meant that the question was about C++. (I actually verified that it wasn't also tagged C before commenting.)
1

Assuming this is a standard .c program, and that your 'name' is a actually a char* then you're going to have to use the strings.h standard c library for the the strcmp() function.

The equality operator == is for std::string and other string classes.

e.g.

if (strcmp(name,"james")==0){
  /* Do stuff */
}

Also, you might consider the switch conditional for multiple tests.

2 Comments

switch requires integral constants. He doesn't have any of these.
Very true. I imagined his strings replaced with equivalent constants (int JAMES=1 etc.) might be better given the seemingly fixed flow of control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.