57

我是一个新的 C 程序员,我想知道如何将一个struct传递给一个函数。我遇到了一个错误,无法找出正确的语法来做到这一点。这是它的代码....

结构:

struct student{
    char firstname[30];
    char surname[30];
};

struct student person;

称呼:

addStudent(person);

原型:

void addStudent(struct student);

和实际功能:

void addStudent(person)
{
    return;
}

编译器错误:

第 21 行:警告:可疑标签声明:struct student
第 223 行:参数 #1 与原型不兼容:

4

5 回答 5

118

这是struct通过引用传递的方法。这意味着您的函数可以访问函数的struct外部并修改其值。您可以通过将指向结构的指针传递给函数来完成此操作。

#include <stdio.h>
/* card structure definition */
struct card
{
    int face; // define pointer face
}; // end structure card

typedef struct card Card ;

/* prototype */
void passByReference(Card *c) ;

int main(void)
{
    Card c ;
    c.face = 1 ;
    Card *cptr = &c ; // pointer to Card c

    printf("The value of c before function passing = %d\n", c.face);
    printf("The value of cptr before function = %d\n",cptr->face);

    passByReference(cptr);

    printf("The value of c after function passing = %d\n", c.face);

    return 0 ; // successfully ran program
}

void passByReference(Card *c)
{
    c->face = 4;
}

这就是您传递structby 值的方式,以便您的函数接收到的副本struct并且无法访问外部结构来修改它。外部是指功能之外。

#include <stdio.h>


/* global card structure definition */
struct card
{
    int face ; // define pointer face
};// end structure card

typedef struct card Card ;

/* function prototypes */
void passByValue(Card c);

int main(void)
{
    Card c ;
    c.face = 1;

    printf("c.face before passByValue() = %d\n", c.face);

    passByValue(c);

    printf("c.face after passByValue() = %d\n",c.face);
    printf("As you can see the value of c did not change\n");
    printf("\nand the Card c inside the function has been destroyed"
        "\n(no longer in memory)");
}


void passByValue(Card c)
{
    c.face = 5;
}
于 2012-11-25T21:45:02.027 回答
47

行功能实现应该是:

void addStudent(struct student person) {

}

person不是类型而是变量,不能用作函数参数的类型。

此外,请确保您的结构在函数原型之前定义,addStudent因为原型使用它。

于 2012-04-29T05:54:08.287 回答
10

将结构传递给另一个函数时,通常最好按照 Donnell 上面的建议进行,而是通过引用传递它。

这样做的一个很好的理由是,如果您想要进行更改,这些更改将在您返回到创建它的实例的函数时反映出来,这会使事情变得更容易。

这是执行此操作的最简单方法的示例:

#include <stdio.h>

typedef struct student {
    int age;
} student;

void addStudent(student *s) {
    /* Here we can use the arrow operator (->) to dereference 
       the pointer and access any of it's members: */
    s->age = 10;
}

int main(void) {

    student aStudent = {0};     /* create an instance of the student struct */
    addStudent(&aStudent);      /* pass a pointer to the instance */

    printf("%d", aStudent.age);

    return 0;
}

在此示例中,addStudent()函数的参数是指向studentstruct -实例的指针student *s。在main()中,我们创建结构的一个实例,然后使用引用运算符 ( ) 将对student它的引用传递给我们的函数。addStudent()&

addStudent()函数中,我们可以使用箭头运算符 ( ->) 来取消引用指针,并访问它的任何成员(功能上等同于: (*s).age)。

我们在addStudent()函数中所做的任何更改都会在我们返回时反映出来main(),因为指针为我们提供了对内存中存储结构实例的位置的引用student。这由 来说明,printf()在本例中将输出“10”。

如果您没有传递引用,您实际上将使用您传递给函数的结构的副本,这意味着当您返回时不会反映任何更改main- 除非您实现了一种传递新版本的方法结构回到主线或类似的东西!

虽然指针起初可能看起来令人反感,但一旦你了解它们的工作原理以及它们为何如此方便,它们就会成为第二天性,你会想知道没有它们你是如何应对的!

于 2013-03-11T02:43:29.633 回答
4

您需要在 person 上指定一个类型:

void addStudent(struct student person) {
...
}

此外,您可以 typedef 您的 struct 以避免每次使用 struct 时都必须键入它:

typedef struct student{
...
} student_t;

void addStudent(student_t person) {
...
}
于 2012-04-29T06:00:13.143 回答
0

代替:

void addStudent(person)
{
    return;
}

尝试这个:

void addStudent(student person)
{
    return;
}

由于您已经声明了一个名为“student”的结构,因此您不必在函数实现中指定,如下所示:

void addStudent(struct student person)
{
    return;
}
于 2013-11-25T01:28:24.057 回答