Thread: Can't Return/Assign Char Pointer

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should get the "not all paths return a value" in gcc if you use "-Wall" as the compile flags.

    And get_input does have ways of exiting without returning a value, never mind that you never CHECK the value in the calling code, so it will happily continue trying to read new data when it's hit an end of file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I use Visual Studio 2008, but the compiler is irrelevant. You're just not enabling all warnings.
    Your functions returns a value of failure, but not on success, which is kindof bad. It should return a value on success to and you should probably check the return to see if it failed or not.
    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.

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Okay, I'm a newbie as I've said and I'm pretty damn pleased that the prog is working the way I want it to. I'm still a long way from knowing how to optimize or maximize safety or whatever but I am interested in learning this. Could tell me what you mean by "kindof bad"? What's the standard for checking returns for both success and failure? Cheers.
    Last edited by SiliconHobo; 12-14-2007 at 07:26 PM.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Currently, your function is designed to return -1 on failure. However, upon success, it doesn't return anything.

    Since you return something from your function, you need to put it somewhere in your main program; so you might have
    Code:
    error_check = get_input (prompt1, entry.name, sizeof(entry.name));
    If get_input fails, error_check will be -1. If get_input doesn't fail, though, error_check will be ... something. I don't know what, which makes it hard to check for.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    If get_input fails, error_check will be -1. If get_input doesn't fail, though, error_check will be ... something. I don't know what, which makes it hard to check for.
    That's just it. It's undefined. Usually, return values are stored in the eax register (on x86), but if you don't return anything, the current value of the eax register will be interpreted as the return data, which is bad, of course.
    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. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Okay, so this is basically how loose ends are tied anytime a non-void function is called? I added:
    Code:
    if (fgets_ptr==NULL) return -1 
    if (fgets_ptr!=NULL) return  1;
    Suppose I change the call from input () to:

    Code:
    error_check = get_input (prompt1, entry.name, sizeof(entry.name));
    as you suggested. Then, what should I do with error_check? Also, if returning values for success and failure is generally good practice to increase safety, does that mean that void functions should be avoided or is it just of particular concern here because of fgets? Cheers.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you add something like return 0; at the end of the function. If the function succeeds, the execution will eventually reach the end of the function so you can return 0 from there, to indicate success (or any other value of choice, like 1; it's up to you).
    What you should do with error_check? You should check if the function succeeded or not. If it failed, then you probably won't be able to proceed because necessary data is missing.
    IF to continue anyway, there's a good chance the program might crash! And that's a bad thing. Very bad thing. It's better to just pop up an error and abort.

    If you can, all functions should return success or fail. If they cannot, then void is acceptable. You can also require the caller to pass in a variable to set if there was an error or not. In that case, the function could return void. Generally, though, functions return success or not. There's nothing wrong with either way, of course, but returning an error might be better because the caller doesn't have to pass in a variable. It can completely ignore your error you return too; it's up to context, really.
    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.

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. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM