将struct传递给函数

Dan*_*ore 49 c struct function

我是一名新的C程序员,我想知道如何struct将一个函数传递给函数.我收到错误,无法找出正确的语法来执行此操作.这是它的代码....

结构:

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

struct student person;
Run Code Online (Sandbox Code Playgroud)

呼叫:

addStudent(person);
Run Code Online (Sandbox Code Playgroud)

原型:

void addStudent(struct student);
Run Code Online (Sandbox Code Playgroud)

和实际功能:

void addStudent(person)
{
    return;
}
Run Code Online (Sandbox Code Playgroud)

编译器错误:

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

小智 92

这是如何传递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;
}
Run Code Online (Sandbox Code Playgroud)

这是您传递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;
}
Run Code Online (Sandbox Code Playgroud)


MBy*_*ByD 40

行函数实现应该是:

void addStudent(struct student person) {

}
Run Code Online (Sandbox Code Playgroud)

person 不是类型而是变量,您不能将它用作函数参数的类型.

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

  • 一个好主意是使用typedef"命名"结构类型以避免此问题.见http://en.wikipedia.org/wiki/Struct_(C_programming_language) (6认同)

tom*_*990 8

将结构传递给另一个函数时,通常最好像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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

如果您没有传递引用,您实际上将使用传递给函数的结构的副本,这意味着当您返回时不会反映任何更改main- 除非您实现了传递新版本的方法struct返回main或者那些行!

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