【问题标题】:GetUserInfo function获取用户信息函数
【发布时间】:2018-03-26 17:03:13
【问题描述】:

我有一个任务,我必须根据他们的输入打印用户的年龄和姓名。

代码如下:

#include <stdio.h>
#include <string.h>

void GetUserInfo(int* userAge, char userName[]) {
   printf("Enter your age: \n");
   scanf("%d", userAge);
   printf("Enter your name: \n");
   scanf("%s", userName);
   return;
}

int main(void) {
   int userAge = 0;
   char userName[30] = "";
   //insert code here
   printf("%s is %d years old.\n", userName, userAge);
   return 0;
}

除了此处的 //insert 代码外,我无法更改任何代码。 我很确定我必须调用该函数,但我无法弄清楚传递该函数的参数。我试过了

void GetUserInfo(int* userAge, char userName[]);

但您可能会发现,由于 main() 中的初始化,它传递了一个空字符串作为名称和 0 作为年龄。

【问题讨论】:

  • 看来您对编程很陌生。您尝试的答案称为函数声明。您正在寻找的是一个通过引用获取参数的函数调用。我不会给你答案,因为你不会学到任何东西。相反,研究这篇文章,并尝试自己解决这个问题。 tutorialspoint.com/cprogramming/…

标签: c function pointers parameters


【解决方案1】:

函数所需的参数是指向 int 的指针和指向 char 的指针。

GetUserInfo(&userAge, 用户名);

“&”运算符为您提供可用作指针的 userAge 变量的地址。

编译器会负责传递 userName 字符数组的开头作为第二个参数。

【讨论】:

    猜你喜欢
    • 2020-06-30
    • 2011-03-13
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    相关资源
    最近更新 更多