Reputation:
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 ) ;
}
Upvotes: 0
Views: 1376
Reputation: 34839
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;
}
Upvotes: 1
Reputation: 1682
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.
Upvotes: 0
Reputation: 1360
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.
Upvotes: 1