【问题标题】:"overload" functions in c - not knowing an input arguments typec中的“重载”函数-不知道输入参数类型
【发布时间】:2017-07-25 06:53:00
【问题描述】:

出于教育用途,我正在尝试实现一个简单的查找程序(类似于 find(1),但功能要少得多)。

在实现第一个功能“-user”时,我遇到了将两个不同的功能(一次用于 uid,一次用于名称)与一个“控制功能?”结合起来的挑战。我想我正在寻找类似 C# 中的重载函数之类的东西,在 C 中解决此类问题的最佳实践是什么?

我希望有一种解决方法,它允许我的程序在调用它时处理这两个选项: 例如:

  • “my_find -user vincent”
  • “my_find -user 1000”

如果您在我的示例中发现一些编码或样式问题,请指出,我愿意学习!我知道错过了一些干净的内存管理,但稍后会添加。

更新 我正在考虑一个函数,它将参数作为 char 数组处理,并检查每个 char 的 ASCII 代码,如果没有找到“char”,则调用该函数。这样的东西“合法”吗?

#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>

void entries_with_uid(char *init_path, int pattern_uid);
void entries_with_name(char *init_path, char *pattern_name);

int main(int argc, char **argv)
{
    /*
    char *path = argv[1];
    */
    entries_with_name("/home/mario/Documents/codes", "mario");
    entries_with_uid("/home/mario/test_project", 1000);
    return 0;
}

void entries_with_uid(char *init_path, int pattern_uid)
{
    DIR *dir;
    struct dirent *entry;

    dir = opendir(init_path);
    entry = readdir(dir);

    if(dir == NULL) return;
    if(entry == NULL) return;

    do {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;

        char path[1024];
        int len = snprintf(path, sizeof(path)-1, "%s/%s", init_path, entry->d_name);

        path[len] = 0;

        struct stat sb;
        stat(path, &sb);

        if(sb.st_uid == pattern_uid) printf("%s\n", path);

        if (entry->d_type == DT_DIR) entries_with_uid(path, pattern_uid);
    } while (entry = readdir(dir));

    closedir(dir);
}

void entries_with_name(char *init_path, char *pattern_name)
{
    DIR *dir;
    struct dirent *entry;

    dir = opendir(init_path);
    entry = readdir(dir);

    if(dir == NULL) return;
    if(entry == NULL) return;

    do {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;

        char path[1024];
        int len = snprintf(path, sizeof(path)-1, "%s/%s", init_path, entry->d_name);

        path[len] = 0;

        struct stat sb;
        stat(path, &sb);

        struct passwd *pwd = getpwuid(sb.st_uid);

        if(strcmp(pwd->pw_name, pattern_name) == 0) printf("%s\n", path);

        if (entry->d_type == DT_DIR) entries_with_name(path, pattern_name);
    } while (entry = readdir(dir));

    closedir(dir);
}

谢谢!

更新

我同时用一个检查参数中的字母数字字符串的函数解决了这个问题:

int count_alphanumeric(char *string)
{
    int alphanumeric_counter = 0;
    int i = 0;

    while(string[i]!='\0')
    {

     if (string[i] <= 57 && string[i] >= 48)
     {
         i++;
         continue;
     }
     else alphanumeric_counter++;
     i++;
    }
    return alphanumeric_counter;
}

仍在寻找更优雅的选择! ;)

干杯!

【问题讨论】:

  • C 标准的 2011 修订版添加了generic selections。因此可以编写宏来模拟重载函数。不幸的是,行为有点under specified
  • 是否有专业人士最常使用的最佳或最佳实践?
  • 好吧,如果您编写选择以接受 char* 并在传递字符串时始终转换为它(有点麻烦,但可移植),您可以避免歧义。
  • 您的意思是在使用 时将其转换为字符串吗?
  • 为什么不使用清晰的名称? -userid 和 -username?

标签: c parameters arguments parameter-passing


【解决方案1】:

您需要检查用户输入的字符串是否为数字。这与函数重载或任何此类语法糖无关。函数重载基于编译时已知的类型,而不是用户可以键入的内容,因此不要寻找使用它的聪明方法。你现在的想法绝对没问题。

然而,实现不是。 57是什么鬼?为什么有人要记住 ASCII 表?字符常量写在单引号中,例如“0”和“9”。为什么要重新发明轮子呢?废弃它并使用正确的惯用 C 方式。

int is_numeric (const char* s, unsigned long* number_return)
{
     char* endp;
     *number_return = strtoul(s, &endp, 0);
     return (*endp == '\0');
}

【讨论】:

  • 我一直在寻找“正确的惯用 C 方式”!谢谢!
猜你喜欢
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 2019-01-03
相关资源
最近更新 更多