【问题标题】:Passing a structure to a function [closed]将结构传递给函数[关闭]
【发布时间】:2017-09-07 06:47:41
【问题描述】:

所以我在编译这个程序时遇到了问题,我就是不能让它工作,我的意思是如果我把inputstudent() 代码放在main() 里面,会容易得多,但我必须把在函数inputstudent() 中编写代码并从main() 调用。我知道这听起来很简单,但我无法理解。

#include <stdio.h>
#include <stdlib.h>

struct student
{
    char surname[50];
    int age;
    char oname[50];
    char address[50];
};
void displaystudent();
void inputstudent();

int main(){
    struct student s;
    inputstudent(s);
    displaystudent(s);

    return 0;
}
void inputstudent(struct student s){
    printf("Enter the surname: ");
    scanf("%s",s.surname);
    printf("Enter the other name: ");
    scanf("%s",s.oname);
    printf("Enter the age: ");
    scanf("%d",&s.age);
    printf("Enter the address: ");
    scanf("%s",s.address);    
}
void displaystudent(struct student s)
{
    printf("Surname: %s \n",s.surname);
    printf("Oname: %s \n",s.oname);
    printf("Age: %d \n",s.age);
    printf("Address: %s",s.address);
}

【问题讨论】:

  • 请正确格式化您的代码
  • "can't get it to work" 是什么意思?有什么问题?
  • void inputstudent(struct student s) -> void inputstudent(struct student *s)scanf("%s",s.surname); -> scanf("%s",s-&gt;surname);
  • 您可能想尝试这里提出的方法:How to debug small programs
  • 使用像inputstudent(&amp;s);这样的指针

标签: c function structure


【解决方案1】:

在 C 中,参数是按值传递的,因此对函数内部参数所做的任何修改都将是 本地 修改。

让我们看一下以下代码 sn-p,它基本上是您尝试做的一个非常简单的版本:

void GetNumber(int number)
{
   printf("Type a number:\n");
   scanf("%d", &number);   // modifies the local variable `number`?
}
...
int n = 0;
GetNumber(n);

现在调用GetNumber 之后n 的值是多少?

嗯,这不是用户输入的数字,但它仍然是 0,即在调用 GetNumber 之前包含的值 n

你需要的是这个:

void GetNumber(int *pnumber)
{
   printf("Type a number:\n");
   scanf("%d", pnumber);  // modifies the value pointed by the pointer pnumber
}
...
int n = 0;
GetNumber(&n);  // &n is the memory address of the variable n

您需要阅读 C 教科书中有关指针的章节。

其他不太重要的问题

你的原型

void displaystudent();
void inputstudent();

不匹配对应的函数。

【讨论】:

    【解决方案2】:

    您正在通过值传递您的struct,这就是函数不修改它的原因。您应该更改旨在修改 struct 的函数以将 struct 指针作为参数:

    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct student {
        char surname[50];
        int age;
        char oname[50];
        char address[50];
    };
    
    void displaystudent(struct student s);
    void inputstudent(struct student *s);
    
    int main() {
        struct student s;
        inputstudent(&s);
        displaystudent(s);
        return 0;
    }
    
    void inputstudent(struct student *s) {
    
        printf("Enter the surname: ");
        scanf("%s", s->surname);
        printf("Enter the other name: ");
        scanf("%s", s->oname);
        printf("Enter the age: ");
        scanf("%d", &s->age);
        printf("Enter the address: ");
        scanf("%s", s->address);
    
    }
    
    void displaystudent(struct student s) {
        printf("Surname: %s \n", s.surname);
        printf("Oname: %s \n", s.oname);
        printf("Age: %d \n", s.age);
        printf("Address: %s", s.address);
    }
    

    【讨论】:

      【解决方案3】:

      正如 Michael Walz 所说,使用指针来修改函数调用中的结构。此外,您的函数签名和定义不匹配,这就是编译器抱怨的原因:

      #include <stdio.h>
      #include <stdlib.h>
      
      struct student {
          char surname[50];
          int age;
          char oname[50];
          char address[50];
      };
      void displaystudent(struct student* pStudent);
      void inputstudent(struct student* pStudent);
      
      int main() {
          struct student aStudent;
          inputstudent(&aStudent);
          displaystudent(&aStudent);
      
          return 0;
      }
      void inputstudent(struct student* pStudent){
      
          printf("Enter the surname: ");
          scanf("%s", pStudent->surname);
          printf("Enter the other name: ");
          scanf("%s", pStudent->oname);
          printf("Enter the age: ");
          scanf("%d", &pStudent->age);
          printf("Enter the address: ");
          scanf("%s", pStudent->address);
      
      }
      void displaystudent(struct student* pStudent)
      {
          printf("Surname: %s \n", pStudent->surname);
          printf("Oname: %s \n", pStudent->oname);
          printf("Age: %d \n", pStudent->age);
          printf("Address: %s", pStudent->address);
      }
      

      【讨论】:

        【解决方案4】:

        您似乎希望对 inputstudent() 内的结构变量 s 所做的更改反映回原始变量。在这种情况下,您需要将变量的地址而不是其值传递给函数。

        如果您将s 传递给函数而不是其地址,则s 的新副本将在inputstudent() 中生成,并且值将被读入此副本而main()中的s保持不变。

        为了解决这个问题,你给inputstudent()一个指向main()s的指针,并让inputstudent()在读取数据时使用这个地址。这样,对inputstudent() 中的变量所做的更改将反映回main() 中的s

        像这样调用函数

        inputstudent(&s);
        

        要使用指向结构变量的指针访问结构变量的成员,请使用-&gt; 运算符而不是. 运算符。

        void inputstudent(struct student *s){
            printf("Enter the surname: ");
            scanf("%s",s->surname);
            printf("Enter the other name: ");
            scanf("%s",s->oname);
            printf("Enter the age: ");
            scanf("%d",&s->age);
            printf("Enter the address: ");
            scanf("%s",s->address);
        }
        

        此外,地址可能包含scanf() 不会包含的空格。你可以使用fgets()

        【讨论】:

          【解决方案5】:

          您的问题基本上有两个原因。

          1. 函数声明没有任何参数。
          2. 结构是按值传递而不是引用inputstudent 函数。

          您可以通过将声明和定义中的函数原型更改为来解决这两个问题

          void displaystudent(struct student s);
          void inputstudent(struct student &s);
          

          【讨论】:

          • 没有参数的函数原型应该不是问题。无参数表示任意数量的参数,但如果是void inputstudent(void),则表示不允许使用任何参数。
          • C 中没有引用传递,这不是 C++ 问题。
          【解决方案6】:

          您不能在inputstudent() 中使用struct student s 作为参数。它只是一个价值副本。
          您应该使用指针作为参数。

          如:

          void inputstudent(struct student* s)
          {...}
          
          int main(){
              struct student s;
              inputstudent(&s);
              displaystudent(s);
          
              return 0;
          }
          

          【讨论】:

          • 你没有讲完整个故事。
          猜你喜欢
          • 1970-01-01
          • 2012-05-09
          • 1970-01-01
          • 2013-10-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多