C Programming - Issues with return

This is a discussion on C Programming - Issues with return within the C Programming forums, part of the General Programming Boards category; Hello. So i am writing this function for my hangman game, this function is to randomly select a word from ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    C Programming - Issues with return

    Hello. So i am writing this function for my hangman game, this function is to randomly select a word from a text and return the word so it can be used in my other functions for the program. But it wont run for the following errors appear:
    In function `Get_Word':
    warning: return makes integer from pointer without a cast
    warning: function returns address of local variable

    I would greatly appreciate any help fixing the problem;

    insert
    Code:
     int Get_Word(){
        //gets random number using the WordCount function and then goes into the dictionary
        //text file and pulls out the word that corresponds to the random number
        
            int N_Num = WordCount();
            int i, number;
            char SecretWord[15];
        
        //Random number generator
        srand(time(NULL));
            number = rand() % N_Num;
        
        //opens file
        FILE*dict = fopen("dictionary.txt", "r");
        
        //gets the ord from the dictionary file using the random number picked
        for(i=0;  i <= number; i++){
            fgets(SecretWord, 15, dict);
            }
        fclose(dict);//closes file
        
        
    return SecretWord;
    }

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Code:
    int Get_Word(){
    ........//gets random number using the WordCount function and then goes into the dictionary
    ........//text file and pulls out the word that corresponds to the random number
    ..........
    ................int N_Num = WordCount();
    ................int i, number;
    ................char SecretWord[15];
    ..........
    ........//Random number generator
    ........srand(time(NULL));
    ................number = rand() % N_Num;
    ..........
    ........//opens file
    ........FILE*dict = fopen("dictionary.txt", "r");
    ..........
    ........//gets the ord from the dictionary file using the random number picked
    ........for(i=0;.. i <= number; i++){
    ................fgets(SecretWord, 15, dict);
    ................}
    ........fclose(dict);//closes file
    ..........
    ..........
    return SecretWord;
    }
    you declare that the function return an integer, yet you return the pointer to a char array. Btw, don't do that either, you 're creating char inside that function, once return, it will go out of scope ( hence the warning regarding local variable). It's better if you pass in a char pointer and return nothing. Example:
    Code:
    void GetWord(char * SecredWord)
    Last edited by nimitzhunter; 04-21-2012 at 12:07 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    Also srand() should only be called once, usually it is better to call this early in main().

    Jim

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Btw, forgot one thing. If you pass in an array, or pointer to a char, remember to add in the size as well, otherwise you'll be blind to how long that array is.
    Code:
     void GetWord(char * SecretWord, int size_of_secret_word)
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having issues with I/O (fprintf and such) new to programming
    By jdouglas_usn in forum C Programming
    Replies: 4
    Last Post: 11-17-2011, 07:00 AM
  2. Newbie issues programming C w/i Xcode
    By marcus314 in forum C Programming
    Replies: 5
    Last Post: 06-09-2010, 11:44 AM
  3. Replies: 3
    Last Post: 08-17-2009, 08:25 AM
  4. Break statement issues, how to return to start of loop?
    By brdiger31 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2007, 03:29 PM
  5. C Programming issues
    By Vicky B in forum C Programming
    Replies: 13
    Last Post: 09-24-2001, 01:34 PM

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