Thread: Char arrays, pointers and if statements

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    36

    Char arrays, pointers and if statements

    Hello

    I have a program that is working how I want it, but I am distressed by a compiler warning. I therefore assume I have poor code and would like to know if this is the case.

    Basically what I have is the following:
    - A linked list containing these nodes
    Code:
    struct node{
    	int number;
    	char *ID;
    	struct node *next;
    };
    - I then have a function which takes an ID and tries to find if the ID is currently present in the list. If it is, then return the number associated with the ID. Else I need to display the name of the ID that isn't in the list

    Code:
    int Getnumber(char *name[]){
    	search = head;              //set searchptr to head of the list
    	while (search->next != NULL){
    		if (search->ID == *name){
    			return search->number;
    		}
    		search= search->next;
    	}
    	if (search->ID == *name){
    			return search->number;
    	}	
    	else{
    	printf("ID %s is not in the list\n",name);
    	exit(1);
    	}
    }
    Given that I call the function like so : Getnumber(ID); where ID is defined as char *string;

    any idea why I get this warning message?
    Code:
    warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’
    Regards,
    James

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    name, as you have it declared, is an array of strings. If you just wanted name to be a single string, then that's just char *name. (You would then not use *name in all your comparisons.)

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    I do in fact want just a single string, thanks.

    The code now looks like this:
    Code:
    int Getnumber(char *name){
    	search = head;              //set searchptr to head of the list
    	while (search->next != NULL){
    		if (search->ID == name){
    			return search->number;
    		}
    		search= search->next;
    	}
    	if (search->ID == name){
    			return search->number;
    	}	
    	else{
    	printf("ID %s is not in the list\n",name);
    	exit(1);
    	}
    }
    I get no compiler warning, but the code no longer functions correctly. It says it cant find an ID that I know for sure is in the list.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remember that == compares values, and the value of name is a pointer. If you want to compare the content being pointed to, you want to use strcmp.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is because you should be using strcmp() to compare strings, not ==.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    Ah thanks, I never heard of strcmp() before. Usually just deal with numbers!

    code is now working fine, thanks for the fast help!

Popular pages Recent additions subscribe to a feed