是否可以使用 C 为函数中的参数创建默认值?

发布于 2024-12-03 10:35:12 字数 804 浏览 2 评论 0原文

我已经开始学习 C,并且来自 PHP,我讨厌缺少 explode() 所以我决定创建自己的。

这是我到目前为止所得到的:

#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;

    exploded = explode(string, " ");
    printf("%s\n", exploded); /* Should currently return 'is' */

    return 0;
}

到目前为止,它的工作正如我所期望的那样。但是,现在我需要在第一维(实际上是两个维度)中创建一个可变大小的二维数组。

我正在考虑做类似 char *explode(char * toExplode, char * delimiter, int length = strlen (toExplode)) 这样我就可以指定长度或将其设置为默认值。但这当然行不通,我不知道从这里该去哪里。

接下来我可以尝试什么?

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;

    exploded = 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.

What can I try next?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

溺孤伤于心 2024-12-10 10:35:12

您可以传递已知的错误值(通常为 0 或 -1)作为长度,具有多个类似的函数,或使用宏。

如果您走坏值路线,当您调用函数时,您可以给它一个您知道不可能的值,并在函数开始时检查是否有这样的坏值。然后自动计算正确的值并照常继续。这样做的问题是,您现在被迫拥有至少一个坏值(在这种情况下不是问题)。

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

在多个相似函数方法中,每个函数的声明略有不同。它们不能全部具有相同的名称,因为 C 不支持像 C++ 这样的其他语言那样的重载方式。 printf() 函数系列就是一个很好的例子。

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

宏方法有点笨拙,但确实有效。它是前两种方法的结合,其中您可以调用两个不同的函数,但第一个函数会进行预处理,并将错误值自动传递给另一个函数,以便它可以计算出正确的长度值。

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

其工作原理是,如果您只给它前两个参数,则 0 就会作为第三个参数。如果给它三个参数,所有三个参数都会正常传递,并且 0 将作为第四个不可见参数添加到函数堆栈中隐藏。如果传递的参数超过 3 个,则所有额外的参数都将像 0 一样被隐藏。如果您尝试仅传递一个参数,则会收到以下错误:

error: expected expression before ',' token

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
眼藏柔 2024-12-10 10:35:12

不,你不能,但这并不能阻止你推入一个虚拟值(即-1),然后如果该值是-1,那么函数中的第一件事就是将其更改为你想要的任何值。

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.

岁吢 2024-12-10 10:35:12

如果您无法使用 C,那么我建议的解决方案是滚动两个函数;一种有长度,一种没有。

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

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

因此,后者只是为您计算出长度并将其传递给前者并返回结果。

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.

夜还是长夜 2024-12-10 10:35:12

在 C 语言中你不能。

这在 C++ 中是可能的,但例如使用常量值 (-1)。

In C you can't.

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

予囚 2024-12-10 10:35:12

C 中的函数不能重载,也不能有默认参数,这就是 C++。

我能想到的唯一解决方案是使用一个带有默认值的静态局部变量。

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;

   /*...*/
}

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;

   /*...*/
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文