0

Is it possible to get a function to return an int or a char* depending on what happened when executed ? I've tried void but at the compilation I get a lot of warning

Example

void int_char(char *str)
{
    if (I 'm matching a condition in my string)
        return (str);
    else
        return (0);
}

I hope I am clear enough. PS : I'm a beginner in C programming

Thomas Dupond
  • 81
  • 2
  • 8
  • Try returing a pointer to structure containing fields of both types (int and char *) only one of then set to non zero value when required (depeding on Your condition). – Dori Jan 08 '16 at 09:31
  • 1
    Are you only ever going to return the number 0 ? If so, you use a return value of char* , return NULL if your condition does not match, and return buffer if it does match. This makes it trivial to check if your function returned NULL or something else. If you need to return different numbers or a char*, you'll need a different solution though. – nos Jan 08 '16 at 09:33
  • `char * func(char *str){ if(match) return buffer; else return NULL;}` – BLUEPIXY Jan 08 '16 at 09:34
  • @K.Gkinis I didn't but I hazard the guess because that answer can literally be found with 5s google in the official C standard. – Magisch Jan 08 '16 at 10:39
  • Sorry, didn't know that SO was a domain name alias for lmgtfy . com – RaidenF Jan 08 '16 at 10:41
  • Check my answer for a complete explanation of how to return error/success codes and change a char * variable in a function. – RaidenF Jan 08 '16 at 12:00
  • @K.Gkinis Yeah, I've read you post. It learned me quite a few thing. – Thomas Dupond Jan 08 '16 at 12:18

5 Answers5

5

The sane way to design such a function would be:

char* get_buffer (const char *str)
{
  if (I'm matching a condition in my string)
    return buffer;
  else
    return NULL;
}

(assuming buffer is not a local variable)

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • @Abhineet Wow.. bad day? You have enough reputation to edit posts and fix simple typos in pseudo code. In fact one could say that it is _expected_ of you. I'm not quite sure that you understand how a user-moderated community is supposed to work. – Lundin Jan 08 '16 at 11:47
  • 1
    @Abhineet Regarding the local variable side-note, I assume that the reader have a minimum of basic knowledge about C and therefore understands why returning a pointer to a local variable is always a bug. If you didn't know that, I'd recommend reading a beginner-level book and also [this post](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope). – Lundin Jan 08 '16 at 11:48
  • I got confused because you said, `buffer` is not a local var and then used that in the function name. I do edit posts. I was afraid to touch yours, though. Never expect you to do any petty mistake :) Removed my downvote and upvoted of course. – Abhineet Jan 08 '16 at 11:51
  • @sunqingyao NULL is actually most often the null pointer constant 0, you are mixing up the macro NULL and null pointers, [see this](http://stackoverflow.com/questions/32136092/how-to-write-c-c-code-correctly-when-null-pointer-is-not-all-bits-zero/32136460#32136460). It doesn't matter however, just forget about 0 and have the caller check for NULL. – Lundin Jan 11 '16 at 07:33
2

Nope, a function can only ever return values of a single type. But you can use a type specialized for this:

struct result {
    int numeric;
    union {
         const char* string;
         int number;
    } value;
};


struct result
do_something() 
{
    struct result r;

    if (...) {

        r.numeric = 1;
        r.value.number = 10;

    } else {

        r.numeric = 0;
        r.value.string = "Foo!";
    }

    return r;
}
Dirk
  • 30,623
  • 8
  • 82
  • 102
0

Try returning void*. It represents a universal pointer

More info: https://msdn.microsoft.com/en-us/library/fxky5d0w.aspx

You can also try a struct or a union and return one of those. Refer to this for more info

Community
  • 1
  • 1
BlueMoon93
  • 2,910
  • 22
  • 39
  • `void*` is a universal pointer, but he wants to return an `int`. – Barmar Jan 08 '16 at 09:47
  • True. I was thinking about how it could work in other similar situations, where you want to return pointers to different type variables. – BlueMoon93 Jan 08 '16 at 09:54
0

In general, it's not possible - i.e.

char *f() {
  return 13;
}

is not allowed. However, zero is a special case (it denotes a 'null pointer' which does not point to anything). So

char *f() {
  return 0;
}

works. Your code sketch suggests that you want to return a null pointer depending on if some condition is true or not, so that may be just what you want.

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
0

I would recommend to use either a class with respective get/set methods wrapping all required return values, or a union as the return data type. Then semantically it will be more obvious how the returned data is interpreted.

dmi
  • 1,424
  • 1
  • 9
  • 9
  • @Samidamaru You can call them ADTs or whatever makes you feel comfortable then. OO design is not related to a specific language. – Lundin Jan 08 '16 at 09:34
  • Ah, didn't notice that this is native C. Then `union` would be the solution. One field - of a type of int, another - char*, and the third field - the flag which field needs to be used. – dmi Jan 08 '16 at 09:35