Char arrays, pointers and if statements

This is a discussion on Char arrays, pointers and if statements within the C Programming forums, part of the General Programming Boards category; Hello I have a program that is working how I want it, but I am distressed by a compiler warning. ...

  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,185
    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,185
    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
    21,794
    That is because you should be using strcmp() to compare strings, not ==.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21