Thread: warning: passing argument 1 of 'fgets' from incompatible pointer type

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    32

    warning: passing argument 1 of 'fgets' from incompatible pointer type

    When I try and compile a simple c program, I get an error

    warning: passing argument 1 of 'fgets' from incompatible pointer type

    I am compiling with gcc. What am I doing wrong?

    Code:
    #include <string.h>
    #include <stdio.h>
    #define MAX_CHAR 132
    #define MAX_SIZE 65536
    FILE *file;
    
    int main(void)
    {
            file = fopen("words", "r");
            char ch;
            char string[MAX_CHAR];
            char temp[MAX_CHAR];
            char words[MAX_SIZE][MAX_CHAR];
            register int counter = 0, number = 0, a = 0;
            srand(time(NULL));
            while ( fgets(&string, MAX_CHAR+1, file) != NULL )
            {
                    a=0;
                    while ( string[a++] != '\n' );
                    string[a-1] = 0;
                    strcpy(words[counter++], string);
            }
    
            number = rand() % counter;
            strcpy(string, words[number]);
    
            while (strcmp(temp,string) != 0)
            {
                    printf("Enter a character: " );
                    ch = getc(stdin);
                    a=0;
                    while ( string[a] != 0 )
                    {
                            if ( string[a] == ch )
                                    temp[a] = ch;
                            else
                            {
                                    if ( temp[a] == 0 || temp[a] == ' ' ) 
                                            temp[a] = ' ';
                            }
                            a++;
                    }
                    printf("%s\n", temp);
            }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    I see, I need to take the & away before the string. I need the var name not the address of the var.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The var name used in this context is the address. BTW,
    Code:
    #define MAX_CHAR 132
    
    char string[MAX_CHAR];
    
    while ( fgets(string, MAX_CHAR+1, file) != NULL )
    ...this should not be +1.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    thanks, I'll fix that too.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by sillyman View Post
    When I try and compile a simple c program, I get an error

    warning: passing argument 1 of 'fgets' from incompatible pointer type

    I am compiling with gcc. What am I doing wrong?

    Code:
    #include <string.h>
    #include <stdio.h>
    #define MAX_CHAR 132
    #define MAX_SIZE 65536
    FILE *file;
    
    int main(void)
    {
            file = fopen("words", "r");
            char ch;
            char string[MAX_CHAR];
            char temp[MAX_CHAR];
            char words[MAX_SIZE][MAX_CHAR];
            register int counter = 0, number = 0, a = 0;
            srand(time(NULL));
            while ( fgets(&string, MAX_CHAR+1, file) != NULL )
            {
                    a=0;
                    while ( string[a++] != '\n' );
                    string[a-1] = 0;
                    strcpy(words[counter++], string);
            }
    
            number = rand() % counter;
            strcpy(string, words[number]);
    
            while (strcmp(temp,string) != 0)
            {
                    printf("Enter a character: " );
                    ch = getc(stdin);
                    a=0;
                    while ( string[a] != 0 )
                    {
                            if ( string[a] == ch )
                                    temp[a] = ch;
                            else
                            {
                                    if ( temp[a] == 0 || temp[a] == ' ' ) 
                                            temp[a] = ' ';
                            }
                            a++;
                    }
                    printf("%s\n", temp);
            }
    return 0;
    }
    The & takes the address of the variable. Since string is a char [], then taking its address makes it a char *[] which is an invalid type of pointer (just as the error suggests). Drop the & and the +1. I also recommend naming your variable something else too since string is not a portable name (in C++ its actually a type).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  2. Replies: 5
    Last Post: 08-12-2007, 05:26 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM