Thread: Function not working

  1. #31
    Registered User
    Join Date
    Nov 2005
    Posts
    31
    HEY!! I think I will start a new thread.... so say good night to this one...

  2. #32
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Now, your latest code has many improvements. While it does compile, it does it nowhere near cleanly:
    Code:
    cbadboard.c:15: warning: return type defaults to `int'
    cbadboard.c: In function `main':
    cbadboard.c:31: warning: comparison between pointer and integer
    cbadboard.c: In function `password':
    cbadboard.c:88: warning: implicit declaration of function `strcmp'
    cbadboard.c:96: warning: format argument is not a pointer (arg 2)
    cbadboard.c:113: warning: format argument is not a pointer (arg 2)
    cbadboard.c:127: warning: control reaches end of non-void function
    Now, to see those errors, I had to ask for them. I use gcc, and I pass the -Wall (Warn all) switch to the compiler. As you said, you're using Apple's XCode (never heard of it before), so that doesn't apply to you. I'd look around for options to turn on extra warnings. I personally always ask gcc for all warnings, since they'll usually turn into errors.

    Let's go through those one at a time.
    Code:
    cbadboard.c:15: warning: return type defaults to `int'
    Easy one here. Really points to line 14, the definition of function main(). You declare main with no return type. Bad for any function. Change it to "int main()"

    Code:
    cbadboard.c:31: warning: comparison between pointer and integer
    Simple mistake here. The line in question is:
    Code:
    if (Fpassword == 1);
    You're comparing a FILE * to an integer. What you meant was to compare the return value of password(). You'll either need to store that in a variable, or move the call to password() inside the if() statement.

    Code:
    cbadboard.c:88: warning: implicit declaration of function `strcmp'
    This warning comes from the fact that strcmp() is used before declared or defined. Since it's a standard function, obviously we'll never see the definition, so we need the declaration, which is in string.h. Add an appropriate #include <string.h> to the top of the file, and this error is no more.

    Code:
    cbadboard.c:96: warning: format argument is not a pointer (arg 2)
    This is the big one. This is what is causing your program to crash, and this is what I pointed out back on page one. Line 96:
    Code:
    scanf("%i",PASSWORD);
    PASSWORD is of type int. We're correctly warned here - scanf() will expect a int *, and will interpret the value of PASSWORD as that, causing a segfault. Change PASSWORD to &PASSWORD, and it will work correctly. I also recommend not calling a variable with a name in all caps, this usually indicates that the term is #define'd somewhere.

    Code:
    cbadboard.c:113: warning: format argument is not a pointer (arg 2)
    Same as the previous error.

    Code:
    cbadboard.c:127: warning: control reaches end of non-void function
    Another good call. If all three password attempts fail, execution can and will reach the end of the function. Then what? It's defined as returning an int, but there is no return statement. I've added a "return 1;". Also note that your return statement on line 120 doesn't match up with the other two, and is the only one that returns the correct number. (With your current setup, I'm assuming 0 to be a successful login, and 1 to be failed.)

    The program will function (but not as intended) after correcting these errors. I still recommend looking into ways to split off code: there is a lot of redundancy in the password function. This will make life easier in the long run, and reduce your code. (And I suggest making a backup before making major changes.)
    Last edited by Cactus_Hugger; 11-12-2005 at 08:28 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM