structs and functions

Now that we have the basics of using a struct, what about passing them to a function?

It is possible to pass instances of the struct to a function, either by pass-by-value or pass-by-reference, OR, by returning the struct.

Let’s use the following struct as before, and typedef it to create a “type” named book, and explore all three methods.

struct bookinfo {
    char title[50];
    char author[50];
    char subject[100];
    char isbn[11];
    int year;
};
typedef struct bookinfo book;

1. Passing by value

If the data contained in a struct is to be used within a function, but not modified, then is can be defined as pass-by-value. For example the following function printfBookdata(), prints out the book data.

void printBookdata(book b)
{
    printf("Book information:n");
    printf("Title   : %s\n", b.title);
    printf("Author  : %s\n", b.author);
    printf("Subject : %s\n", b.subject);
    printf("ISBN    : %s\n", b.isbn);
    printf("Year    : %d\n", b.year);
}

ddd

2. Passing by reference

If the data contained in a struct is to be modified within a function, then it should be defined as pass-by-reference. For example, the following function enterBookdata() takes an empty book, and “populates” it with data.

void enterBookdata(book *b)
{
    printf("Book title? ");
    fgets(b->title, 50, stdin);
    printf("Book author? ");
    fgets(b->author, 50, stdin);
    printf("Book subject? ");
    fgets(b->subject, 100, stdin);
    printf("Book ISBN? ");
    fgets(b->isbn, 11, stdin);
    printf("Year of publication? ");
    scanf("%d", &b->year);
}

This function is called in the following manner:

book abook;
enterBookdata(&abook);

Note that there are some differences in the way things are structured inside the function. Because the book, b is a pointer to the struct, “->” has to be used instead of “.” to qualify the members.

3. Passing information back using return

The final way of returning things is by using the return statement. Here is the same function as above modified to account for this scenario.

book enterBookdata()
{
    book b;

    printf("Book title? ");
    fgets(b.title, 50, stdin);
    printf("Book author? ");
    fgets(b.author, 50, stdin);
    printf("Book subject? ");
    fgets(b.subject, 100, stdin);
    printf("Book ISBN? ");
    fgets(b.isbn, 11, stdin);
    printf("Year of publication? ");
    scanf("%d", &b.year);
 
 return b;
}

Note that a local struct, b,  is created. It is then populated (and as it is local it uses the “.” separator). Then the struct is returned. The calling end of the function looks like this:

book b;
b = enterBookdata();

A pointer is not needed in this case.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s