简体   繁体   English

将结构变量传递给函数

[英]passing structure variable to function

I'm studying on structures in C programming. 我正在研究C编程中的结构。 But, I'm confused in this code so that I don't understand. 但是,我对这段代码感到困惑,以至于我不明白。 From where is the b coming in the function ? b从哪里来? How can be a structure used like this ? 如何使用这样的结构? Could you explain me ? 你能解释一下我吗? Can we say display(struct book b1) ; 我们可以说 display(struct book b1) ; calling the function ? 调用函数? Thank you for all appreciated answers. 感谢您提供所有赞赏的答案。

#include <stdio.h>

struct book
{
    char name[25] ;
    char author[25] ;
    int callno ;
} ;
int main()
{
    struct book b1 = { "Let us C", "YPK", 101 } ;
    display ( b1 ) ;

    return 0;
}

void display ( struct book b )
{
    printf ( "\n%s %s %d", b.name, b.author, b.callno ) ;
}

b is the name of the parameter for the display function. b是显示功能的参数名称。 It is what you have to pass to it. 这是您必须传递给它的。 So in the main function when you call display(b1); 因此,在主函数中,当您调用display(b1); b in the display function represents the book structure defined by b1 in the main function. 显示功能中的b表示主要功能中b1定义的书籍结构。

My best guess is that you are confused by the similarity of the variable name in main b1 , and the parameter name in the function b . 我的最佳猜测是,您对main b1中的变量名和函数b的参数名的相似性感到困惑。 Those names are completely unrelated, and could be called anything you like. 这些名称是完全无关的,可以随意命名。

In the main function, b1 is a local variable that is declared as type struct book , and then initialized with a compile time constant initializer. main函数中, b1是一个局部变量,它声明为struct book类型,然后使用编译时常数初始化程序进行初始化。 The name b1 is arbitrary and can be any valid identifier. 名称b1是任意的,可以是任何有效的标识符。

In the display function, b is an argument to the function of type struct book . display函数中, bstruct book类型的函数的参数。 When the function is called, the caller must provide a struct book , and that struct will be copied into b . 调用函数时,调用者必须提供一本struct book ,并将该结构复制b It's important to understand that b is a copy of the struct that was passed to the display function, which means that any changes that b makes to its local copy will not be propagated to the original structure declared in main . 重要的是要了解b是传递给display函数的结构的副本,这意味着b对其本地副本所做的任何更改都不会传播到main声明的原始结构。

Here's an attempt to demonstrate these principles in code 这是尝试在代码中演示这些原理的尝试

#include <stdio.h>

struct book
{
    char name[25] ;
    char author[25] ;
    int callno ;
};

void display( struct book someArbitraryNameForTheLocalCopyOfTheStructThatThisFunctionUses )
{
    printf ( "%s  ", someArbitraryNameForTheLocalCopyOfTheStructThatThisFunctionUses.name   );
    printf ( "%s  ", someArbitraryNameForTheLocalCopyOfTheStructThatThisFunctionUses.author );
    printf ( "%d\n", someArbitraryNameForTheLocalCopyOfTheStructThatThisFunctionUses.callno );

    // the following line has no effect on the variable in main since
    // all we have here is a local copy of the structure
    someArbitraryNameForTheLocalCopyOfTheStructThatThisFunctionUses.callno = 5555;
}

int main()
{
    struct book someLocalVariable = { "Let us C", "YPK", 101 };

    // the following line will make a copy of the struct for the 'display' function
    display( someLocalVariable );

    // the following line will still print 101, since the 'display' function only changed the copy
    printf ( "%d\n", someLocalVariable.callno );

    struct book anotherBook = { "Destiny", "user3386109", 42 };

    // any variable of type 'struct book' can be passed to the display function
    display( anotherBook );

    return 0;
}

Passing structures works like passing any other type: The function expects a variable of type struct b as it's argument, and then it just works with it. 传递结构的方式就像传递任何其他类型一样:函数将参数struct b类型的变量作为参数,然后就可以使用它。 What is happening behind the scenes is, that all the data of b1 in your main function is copied into b of your display function. 幕后发生的事情是,将主函数中b1所有数据都复制到了显示函数的b中。 So be careful about that: When you change the value of a member of b in display , it won't change the value of b1 in main . 因此,请注意以下几点:当更改displayb的成员的值时,它不会更改mainb1的值。 If you want that to happen, you have to pass a pointer. 如果您希望这种情况发生,则必须传递一个指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM