// This program demonstrates how to access pointers of type struct. // Usually we access with a . , however, when we refer to a pointer // we access with a -> (shorthand, much less likely to make mistakes!) // Sasha Vassar, Week 5 Lecture 10 #include // we will want to use strcpy function, so get access to the string.h library // for this particular example #include #define MAX 35 // 1. Define struct struct lego { char name[MAX]; double price; int pieces; }; int main (void) { // 2. Declare struct struct lego my_lego; //my_lego.price // TODO: Demonstrating pointers of type struct // Have a pointer that points to the variable my_lego of type // struct lego // pointer to an int: int *my_ptr struct lego *my_lego_ptr = &my_lego; // TODO:3. Initialise struct (access members with .) my_lego_ptr->price = 120.5; my_lego_ptr->pieces = 2064; strcpy(my_lego_ptr->name, "Question Mark"); // TODO: Demonstrating pointers of type struct - accessing // How would we initialise it using the pointer? // Perhaps dereference the pointer and access the member? printf("The lego %s costs %.2lf and has %d pieces\n", my_lego_ptr->name, my_lego_ptr->price, my_lego_ptr->pieces); return 0; }