开发者

Is it possible to create a default value for an argument in a function with C?

Alright, so forgive me. I've started learning C, and coming from PHP, I hate the lack of explode() so I decided to create my own.

Here's what I have so far:

#include <stdio.h>
#include <windows.h>

char * explode(char * toExplode, char * delimiter) /* Need to add a buffer size here */
{
    char * token;
    token = strtok(toExplode, delimiter);
    token = strtok(NULL, delimiter);

    return token;
}

int main(void)
{
    char string[] = "This is a string yaaaaay";
    char * exploded;

    expl开发者_JS百科oded = explode(string, " ");
    printf("%s\n", exploded); /* Should currently return 'is' */

    return 0;
}

So far, it's working just as I expect it to. However, now I need to create a 2D array of variable size in the first dimension (actually, both dimensions.)

I was thinking of doing something like char * explode(char * toExplode, char * delimiter, int length = strlen(toExplode)) so that I could either specify the length or have it set it default. This of course doesn't work though, and I have no idea where to go from here.

Any ideas/help?


You can pass a known bad value (commonly a 0 or -1) as the length, have multiple similar functions, or use a macro.

If you go the bad value route, when you call your function you can give it a value you know isn't possible and check for such a bad value at the start of the function. Then automatically calculate the correct value and continue as usual. The problem with this is that you are now forced to have at least one bad value (not a problem in this case).

char * explode(char * toExplode, char * delimiter, int length){
    if(length == 0)
        length = ...;

In the multiple similar functions method, each function has a slightly different declaration. They cannot all have the same name because C does not support overloading the way that another language like C++ has. The printf() family of functions is a good example of this.

char * explodel(char * toExplode, char * delimiter, int length);
char * explode (char * toExplode, char * delimiter){
    int length = ...;
    return explodel(toExplode, delimiter, length);
}

The macro method is a bit of a hack, but it does work. It is a bit of a combination of the previous two methods, in which you have the two different functions you can call, but the first one gets preprocessed with a bad value automatically being passed to the other so it can figure out the correct length value.

#define explode (s, ...) explodel(s, __VA_ARGS__, 0)
char *  explodel(char * toExplode, char * delimiter, int length, ...);

The way this works is that if you only give it the first two arguments, the 0 fall into place as the third argument. If you give it three arguments, all three are passed normally and the 0 is added as a forth invisible argument hidden away in the function stack. If you pass more than three, all the extra arguments will be hidden like the 0. If you try to pass only one argument, you will get the following error:

error: expected expression before ',' token


No you can't but that doesn't stop you from pushing in a dummy value (i.e. -1) and then first thing in the function if the value is -1 then change it to whatever value you want.


If you're stuck using C then the solution I'd recommend is rolling two functions; one with length, one without.

char * explode(char * toExplode, char * delimiter, int length)
{
   ...
}

char * explode(char * toExplode, char * delimiter)
{
   int len = strlen(toExplode);
   return explode(toExplode, delimiter, len);
}

So, the latter just works out the length for you and passes it to the former and returns the result.


In C you can't.

This is possible in C++, but with a constant value (-1), for example.


You can't overload a function in C, neither have default arguments, that's C++.

The only solution I can think of is having an static local variable, with the default value.

char * explode(char * toExplode, char * delimiter,unsigned int plen) /* Need to add a buffer size here */
{
    static unsigned int slen = 100;
    unsigned int len; 
    char * token;

    if (plen!=0)
        len = plen;
    else
        len = slen;

   /*...*/
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜