Untitled

mail@pastecode.io avatar
unknown
c_cpp
17 days ago
785 B
2
Indexable
Never
#include <stdio.h>
#include <string.h>

typedef struct
{
    char title[ 50 ];
    int page_count;
} Book;

Book initBook( char title[], int page_count )
{
    Book newBook;

    strncpy( newBook.title, title, 49 );
    newBook.title[ 49 ] = '\0';
    newBook.page_count = page_count;

    return newBook;
}

void showBook( Book* book )
{
    printf( "Title: %s\n", book->title );
    printf( "Page count: %d\n", book->page_count );
}

void addPages( Book* bookPtr )
{
    bookPtr->page_count += 10;
}

int main()
{
    Book myBook = initBook( "Sample Book", 100 );

    printf( "Before adding pages:\n" );
    showBook( &myBook );

    addPages( &myBook );

    printf( "After adding pages:\n" );
    showBook( &myBook );

    return 0;
}
Leave a Comment