c语言结构体函数传递方式,c-将struct传递给函数

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

#include

/* 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;

}

这就是通过值传递struc

你可能感兴趣的:(c语言结构体函数传递方式)