0

I'm studying on structures in C programming. But, I'm confused in this code so that I don't understand. From where is the b coming in the function ? How can be a structure used like this ? Could you explain me ? Can we say 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 ) ;
}
5
  • Do you have any problem with void display (int i) { ... }? Commented Dec 14, 2014 at 21:26
  • Can you please explain why it can't? Commented Dec 14, 2014 at 21:27
  • Can we say struct book b1 ; calling the function ? Commented Dec 14, 2014 at 21:31
  • I still don't get what is your confusion. Commented Dec 14, 2014 at 21:32
  • b is a value-parameter automatic variable in display(), copied from b1 in main() when the call is performed. It really is that simple. What about that don't you understand? (and possibly related to your question, the warnings you (better) be getting about display() not being declared before use and having an assumed int return value, and not matching that implicit declaration when you finally encounter it, can be fixed by either properly prototyping display or by moving its definition above main()). Commented Dec 14, 2014 at 21:33

3 Answers 3

1

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. 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. The name b1 is arbitrary and can be any valid identifier.

In the display function, b is an argument to the function of type struct book. When the function is called, the caller must provide a struct book, and that struct will be copied into 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.

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

b is the name of the parameter for the display function. It is what you have to pass to it. So in the main function when you call display(b1); b in the display function represents the book structure defined by b1 in the main function.

Comments

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. 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. 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. If you want that to happen, you have to pass a pointer.

Comments