Thread: warning: return makes pointer from integer without a cast

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    warning: return makes pointer from integer without a cast

    Hello all,

    I have a function that I want to exit gracefully when an "error" occurs in an input file. My function declaration is:
    Code:
    BSTnode *buildTree(FILE *fp)
    The few lines that are causing the problems are:
    Code:
    if(regcomp(&regex, to_find, REG_EXTENDED | REG_NEWLINE) != 0)
        {
            fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
            return EXIT_FAILURE;
        }
    I know that if I just use "return" by itself the warning goes away but fails to exit when the error occurs. I also believe this may not be the correct use of stderr. But I need the program to exit when an error has occurred.
    Can someone show and explain a better & correct way of doing this?
    Thank You.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're returning an integer.
    Since the function returns a pointer, the usual error value is NULL.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by oogabooga View Post
    You're returning an integer.
    Since the function returns a pointer, the usual error value is NULL.
    So are you saying:
    Code:
    return NULL;
    or just
    Code:
    return;
    ?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you compile with a decent warning level you will get a warning if you just "return". If the function says that it returns something, then you have to return something (of that type).
    So use "return NULL;".
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by oogabooga View Post
    If you compile with a decent warning level you will get a warning if you just "return". If the function says that it returns something, then you have to return something (of that type).
    So use "return NULL;".
    Makefile:
    Code:
    CC = gcc
    CFLAGS = -Wall -c
    LDFLAGS =
    SOURCES = main.c fileCheck.c buildTree.c traverseInorder.c traversePreorder.c traversePostorder.c
    OBJECTS = $(SOURCES:.c = .o)
    DEPS = main.h node.h fileCheck.h
    EXECUTABLE = tree
    
    all: $(SOURCES) $(EXECUTABLE)
    
    $(EXECUTABLE): $(SOURCES) $(DEPS)
            $(CC) $(LDFLAGS) $(OBJECTS) -o $@
    
    .c.o:
            $(CC) $(CFLAGS) $< -o $@
    
    .PHONY: clean
    
    clean:
            rm -f $(EXECUTABLE) *.o
    What other "decent" compiler warning flags are there?

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    -Wall should be good enough.
    If you just "return" in your function it should say "warning: 'return' with no value in function returning non-void".
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    -Wextra does stuff, right?

    And I swear to God, I've seen some with the -03 or -00 flags or something. Okay, my Makefiles have it but only because I've seen others use it. Granted, the only time I ever looked at C code was astrophysics research code so Idk if they knew what it did but I just copy them XD

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by oogabooga View Post
    -Wall should be good enough.
    If you just "return" in your function it should say "warning: 'return' with no value in function returning non-void".
    Thanks!

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by MutantJohn View Post
    -Wextra does stuff, right?

    And I swear to God, I've seen some with the -03 or -00 flags or something. Okay, my Makefiles have it but only because I've seen others use it. Granted, the only time I ever looked at C code was astrophysics research code so Idk if they knew what it did but I just copy them XD
    I did add -Wextra to my Makefile.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by MutantJohn View Post
    -Wextra does stuff, right?

    And I swear to God, I've seen some with the -03 or -00 flags or something. Okay, my Makefiles have it but only because I've seen others use it. Granted, the only time I ever looked at C code was astrophysics research code so Idk if they knew what it did but I just copy them XD
    The -O flags are "optimization" flags and probably shouldn't be used until you've debugged the program.
    -Wextra enables a few more warnings but they are not the most useful ones. -pedantic ensures(?) complete conformance to the standard. I don't like it with C89 since it complains about // comments which are pretty useful and likely to be supported by any popular compiler. Still, it's probably a good idea for C89. But people should probably be using C99 today, which requires the -std=c99 flag.

    From Warning Options - Using the GNU Compiler Collection (GCC)
    -Wextra
    This enables some extra warning flags that are not enabled by -Wall. (This option used to be called -W. The older name is still supported, but the newer name is more descriptive.)
    -Wclobbered
    -Wempty-body
    -Wignored-qualifiers
    -Wmissing-field-initializers
    -Wmissing-parameter-type (C only)
    -Wold-style-declaration (C only)
    -Woverride-init
    -Wsign-compare
    -Wtype-limits
    -Wuninitialized
    -Wunused-parameter (only with -Wunused or -Wall)
    -Wunused-but-set-parameter (only with -Wunused or -Wall)


    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-12-2011, 07:33 PM
  2. Replies: 1
    Last Post: 08-11-2009, 12:09 PM
  3. Replies: 4
    Last Post: 03-10-2009, 10:29 AM
  4. Replies: 17
    Last Post: 02-13-2006, 01:19 PM
  5. Replies: 3
    Last Post: 01-14-2002, 12:13 PM