Thread: Incompatible Return Type Question

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    1

    Incompatible Return Type Question

    Hi there!

    Sorry for the rather simple question, I rarely ever code in plain C.

    But essentially, I have something that looks like this:

    Code:
    // This is malloc'd and initialized somewhere, I promise!
    static char **somestrings = NULL;
    
    
    const char **getsomestrings(void)
    {
        return somestrings;
    }
    The compiler throws a warning at me "returning ‘char **’ from a function with incompatible return type ‘const char **’"

    Is it safe to just cast this like so:

    Code:
    return ( const char** )somestrings;
    Or is there a legitimate reason the return type is considered "incompatible"?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In this case it is safe because you're going from something that can be modified to a const context. It is the vice versa situation that can be unsafe, e.g., because it could lead to an attempt to modify a string constant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-02-2014, 05:29 PM
  2. incompatible type return
    By convenientstore in forum C Programming
    Replies: 11
    Last Post: 10-16-2010, 11:36 PM
  3. Return from incompatible pointer type
    By aladin in forum C Programming
    Replies: 2
    Last Post: 03-22-2009, 08:58 AM
  4. incompatible type
    By desertstorm in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 03:23 PM
  5. Question for return type.
    By vnrabbit in forum C Programming
    Replies: 9
    Last Post: 08-25-2003, 07:39 AM

Tags for this Thread