Thread: Comparison between pointer & integer

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    28

    Comparison between pointer & integer

    Hello!

    I am trying to create a loop that searches a struct array for the value of 'remove' (which is a char).

    Struct:

    Code:
    typedef struct
    {
        char track[99];
        char trackname[99]; 
        char artist[99];
        char genre[99];
    }   song; //creates a struct called song;
    
    song songs[99];

    Code to search for a track name:

    Code:
               int i;
                        for(i=0;i<99;i++)
    
                        {
                            if(*songs[i].trackname == remove)
        	                {	
                                 printf("found it");
                                }
        	                }
                        }
    The problem is that I am told 'comparison between pointer and integer'. I could remove the '*', but will then be told 'comparison of distinct pointer types lacks a cast'.

    I am assuming that 'song[i] and trackname are variable types that cannot be compared? I don't really know what the '*' does, but I saw it on some site discussing this problem so thought I might try it lol.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    need to use strcmp() for comparing strings!

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You shouldn't name something “remove”, as that's the name of a standard function. It sounds to me like the comparison is using the standard remove, and not yours, according to the error messages. Of course, without a complete program I'm only left guessing.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    I didn't realise 'remove' is a standard function, so have renamed it to 'aremove'. I called it that because once I get this working, I want to try change it so that it can remove a song in the struct.

    How would the strcmp() be used? I didn't know I was comparing strings lol.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know how remove is declared, so I cannot say if it's a string or not. Anyway, strcmp is used like this:
    strcmp(str1, str2)
    It returns 0 if the strings are equal.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    28
    Code:
    strcmp(songs[i], aremove)==0;
    And I am told 'incompatible type for argument 1 of 'strcmp'.

    FYI, this is the declaration of 'aremove' (other variables have been deleted to make this shorter).

    Code:
    int main(void)
    
    {
        system("color f5");
        
        int  aremove;
    I could paste the code, but it's 137 lines and I'm not sure if it would be a pain to go through.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM