Like Tree1Likes
  • 1 Post By AndiPersti

Strcmp makes pointer from integer without a cast

This is a discussion on Strcmp makes pointer from integer without a cast within the C Programming forums, part of the General Programming Boards category; I keep getting an error on the lines with strcmp that passing argument 3 of binsearch makes pointer from integer ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    21

    Strcmp makes pointer from integer without a cast

    I keep getting an error on the lines with strcmp that passing argument 3 of binsearch makes pointer from integer without a cast.
    How can I fix this?

    Code:
    void binsearch(char *dictionary, int wordnum, char string[]) {
    
       //Establish low and high points
        int low = 0;
        int high = wordnum - 1;
    
        //Adjust midpoint as necessary
        while (low <= high) {
        int mid = (low + high)/2;
        if (strcmp(string, dictionary[mid])<0)
            high = mid - 1;
        else if (strcmp(string, dictionary[mid]) > 0)
            low = mid + 1;
    
        //Print out correct permutation matches
        else if (strcmp(string, dictionary[mid]) == 0){
            fprintf(ofp, "A permutation of the current word that is valid is %s.\n", string);
            break;
         }
        }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    strcmp takes two strings. dictionary[mid] is a char, not a char*.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    So do I just put a * in front of dictionary[mid]?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by krazzyjman View Post
    So do I just put a * in front of dictionary[mid]?
    That would be trying to dereference a char, which doesn't make sense. It seems like you want dictionary to be declared "char *dictionary[]" instead of "char *dictionary", but aside from other speculation I can't really help you.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Ok, thank you.
    Now am I using the bin search in the correct way?
    Code:
    for (j=k; j<strlen(str); j++) {
                    ExchangeCharacters(str, k, j);
                    binsearch((str), k, j);
                    RecursivePermute(str, k+1);
                    ExchangeCharacters(str, j, k);
                    binsearch((str), k, j);

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    Here is my full function, I am still getting a few errors when I try to compile
    Code:
    void binsearch(char *dictionary[], int wordnum, char string[]);
    Code:
    void binsearch(char *dictionary[], int wordnum, char string[]) {
    
       //Establish low and high points
        int low = 0;
        int high = wordnum - 1;
    
        //Adjust midpoint as necessary
        while (low <= high) {
        int mid = (low + high)/2;
        if (strcmp(string,dictionary[mid])<0)
            high = mid - 1;
        else if (strcmp(string,dictionary[mid]) > 0)
            low = mid + 1;
    
        //Print out correct permutation matches
        else if (strcmp(string,dictionary[mid]) == 0){
            printf("A permutation of the current word that is valid is %s.\n", string);
            fprintf(ofp, "A permutation of the current word that is valid is %s.\n", string);
        }
        else {
            printf("This word has no permutations\n");
        }
            break;
    
        }
    }
    any suggestions, I cannot figure it out

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,612
    Quote Originally Posted by krazzyjman View Post
    Here is my full function, I am still getting a few errors when I try to compile


    any suggestions, I cannot figure it out
    I suggest posting the error messages.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    21
    |In function 'main':|
    |41|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[20]'|
    ||In function 'RecursivePermute':|

    |69|warning: comparison between signed and unsigned integer expressions|

    |71|warning: passing argument 1 of 'binsearch' from incompatible pointer type|
    |9|note: expected 'char **' but argument is of type 'char *'|

    |71|warning: passing argument 3 of 'binsearch' makes pointer from integer without a cast|

    |9|note: expected 'char *' but argument is of type 'int'|

    C|74|warning: passing argument 1 of 'binsearch' from incompatible pointer type|

    |9|note: expected 'char **' but argument is of type 'char *'|

    |74|warning: passing argument 3 of 'binsearch' makes pointer from integer without a cast|

    9|note: expected 'char *' but argument is of type 'int'|
    ||=== Build finished: 0 errors, 6 warnings ===|

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    At a minimum, you have to also post the call to this function and the types of the arguments to that call. Optimally you'd post your entire program.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    void binsearch(char *dictionary[], int wordnum, char string[]);
    Code:
    for (j=k; j<strlen(str); j++)
     {    
            ExchangeCharacters(str, k, j);
            binsearch((str), k, j);
    You define binsearch() to take an array of pointers to char (i.e. pointer to pointer to char aka char **), an int and an array of char (i.e. pointer to char aka char *).
    But you call it with a string "str" (i.e. char *), an int "k" and another int "j". (the variable types are my guesses based on your earlier usage).

    Do you see your problem? Do you understand the warning messages now?

    Bye, Andreas
    stahta01 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strcpy makes pointer from integer without a cast?
    By Kalastrian in forum C Programming
    Replies: 11
    Last Post: 01-07-2012, 05:37 AM
  2. 'Makes pointer from integer without a cast'
    By idlackage in forum C Programming
    Replies: 5
    Last Post: 06-10-2010, 05:48 AM
  3. Replies: 6
    Last Post: 05-12-2010, 10:26 AM
  4. makes pointer from integer without a cast
    By nyquil in forum C Programming
    Replies: 5
    Last Post: 05-06-2010, 06:40 AM
  5. Help: makes pointer from integer without a cast
    By steffi in forum C Programming
    Replies: 4
    Last Post: 11-14-2007, 03:38 AM

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