Hello, and welcome back.
We have another challenge for you.
This challenge is going to be about structures again.
It's going to get you familiar with elements in a structure
that contain a pointer.
So you have to allocate memory for that element.
It's also going to get you familiar with declaring a structure as a
pointer and also passing a structure as a pointer to a function.
So mainly structures and pointers in this challenge.
So you need to write a c program that creates a structure pointer
and passes it to a function.
Create a structure named item with the following members.
An item name which should be a character pointer.
A quantity, which should be an int.
A price should be a float and the amount should be a float.
The amount that's stored in the structure is going to essentially be
the quantity times the price because that would be the total amount.
I want you to create two functions.
I want you to create a function named read item that takes a
structure pointer of type item as a parameter should be a pointer.
We know the advantages of functions taking pointers as arguments as
opposed to a regular structure, mainly being they're backward compatible.
Previous versions of the c compiler didn't work with structures.
And it's also a lot faster and more efficient when you pass a
pointer to a function because you don't have to copy all the
elements, you just copy the address.
So we're going to have functions that take as parameters
pointers to structures.
This read item is going to take on.
What this function should do is, it should read in from the
user a product name, a price and a quantity or an item name.
The contents right in should be stored in the past in structure.
So you're passing in a pointer to a structure you
want to store it in there.
I want you to create another function named print item.
This also takes as a parameter structure pointer of type item.
This function will print out the contents of the parameter.
So you're just going to do some printfs.
That will be pretty basic.
In the main function, you want to declare an item basically an
item of of the pointer structure or declare an item and a pointer
of the structure because then you can assign to the point of the
item, the address of the item.
And then you also need to allocate memory for the item name pointer.
So remember, the item name element inside the structure is
character pointed that means you have to allocate memory for it.
If it was a character array, you wouldn't have to.
But remember, when you have an element as a character pointer, all it does
is create memory for the actual address, the value of that pointer.
It doesn't create memory for what it's pointing to.
So if you assign a literal constant to it, it could be anywhere in memory.
So you want to allocate memory for that item name
element inside the structure.
The item pointer should be passed into both the reading
the print item functions.
So again what this challenge will help you with is understanding pointers
how to use them as a member inside of a structure and then also how to pass
them around and you have some practice with doing some malloc as well.
And then don't forget, whenever you do a malloc, you have to
do a free, so make sure you add that to your program.
So good luck.
I'll provide a solution in the next lecture.
As far as mallocing the size, you can just pick an arbitrary size such as
30 characters or something like that.
You don't have to read it in.
Let me know if you have any questions. Thank you.