【问题标题】:Need help with declarations in C需要 C 语言声明方面的帮助
【发布时间】:2009-12-02 15:41:40
【问题描述】:

如何进行以下声明?

int main(int argc, char *argv[]) {
char *users[] = {};
char *names[] = {};

openPasswd(users, names);

return 0;
}

void openPasswd(char &users[] = {}, char &names[] = {}){
}

我想将函数中的 2 char arrays 填充回主程序。

我该怎么做?

【问题讨论】:

  • 使用选择代码块编辑您的代码块,然后按 CTRL+K
  • argc,argv!那是什么代码!... :) 请相应地对其进行编辑,并确保您的问题清楚,以便其他人理解和回答!
  • 这里没有 char 数组 - 你有 char 指针数组,这是不同的野兽。

标签: c arrays pointers


【解决方案1】:

你需要使用pointers-to-pointers-to-pointers:

void openPasswd(char ***users, char ***names)
{
  *users = malloc(3 * sizeof **users);
  (*users)[0] = "3";
  (*users)[1] = "4";
  (*users)[2] = "7";
  *names = malloc(3 * sizeof **names);
  (*names)[0] = "foo";
  (*names)[1] = "bar";
  (*names)[2] = "baz";
}

int main(void)
{
  char **users, **names;

  openPasswd(&users, &names);
  print("user %s is named %s\n", users[0], names[0]);
  return 0;
}

请注意,上面省略了错误检查,并假定数据在编译时当然是已知的。

【讨论】:

  • 天啊! c2.com/cgi/wiki?ThreeStarProgrammer!但这似乎是在普通的旧 C 中最明显的方法
  • @Johan:嗯......如果你认为指针很难,或者同意它不是“恭维”的想法,那么也许 C 不是你的语言。或者,嘿,也许您更喜欢使用 typedef 隐藏它们。 :)
  • 谢谢!!!它看起来很棒,但它不起作用...... :( 我得到“来自不兼容的指针类型的赋值”......
  • openPasswd() 函数中的 users[n]names[n] 应该是 (*users)[n](*names)[n]。您想为指向的用户和名称下标。
  • @Johan:回复:那个链接 - 老实说,我不明白为什么人们会如此依赖多个间接级别。两星、三星、十星,都遵循同样的规则。它可能难以阅读,但并不难理解。
【解决方案2】:

如果您提前不知道数组会有多大:

int main(int argc, char **argv) 
{
  char **users = NULL; // users and names will be dynamically allocated arrays
  char **names = NULL; // of pointer to char
  size_t entries = 0;
  /**
   * Somewhere between here and openPassword figure out how big the arrays
   * need to be
   */
  openPasswd(&users, &names, entries);
  return 0;
}
/**
 * Since we need to modify the values of the pointers for users and names,
 * we must pass pointers to those pointers.
 */
void openPasswd(char ***users, char ***names, size_t entries)
{
  size_t i;

  /**
   * allocate the arrays
   *
   * type of  *users == char **
   * type of **users == char *
   */
  *users = malloc(sizeof **users * entries);
  *names = malloc(sizeof **names * entries);
  /**
   * Allocate each entry and get the username/password data from somewhere
   * get_user_length, get_name_length, get_user, get_name are all
   * placeholders.  
   */
  for (i = 0; i < entries; i++)
  {
    /**
     * The subscript operator should not applied to the values of users and 
     * names, but to the values of what users and names *point to*.  Since 
     * [] binds before *, we must use parens to force the correct grouping.  
     *
     * type of  (*users)[i] == char *
     * type of *(*users)[i] == char
     */
    (*users)[i] = malloc(sizeof *(*users)[i] * get_user_length(i));
    if ((*users)[i] != NULL)
    {
      strcpy((*users)[i], get_user(i));
    }
    (*names)[i] = malloc(sizeof *(*names)[i] * get_name_length(i));
    if ((*names)[i] != NULL)
    {
      strcpy((*names)[i], get_name(i));
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多