Question
IN C PROGRAMMING Structure and pointer In this problem set, we will learn about array of structure variables along with the following subjects: 1. structure
IN C PROGRAMMING
Structure and pointer In this problem set, we will learn about array of structure variables along with the following subjects:
1. structure - struct
2. declaration of array of structure and typedef struct
3. using rand()
4. pointer, pointer arithmetic in a for loop,
5. pass by reference vs. pass by value
6. passing and using a pointer as a function return value of the structure
7. not using magic numbers
8. using #if and #endif in c programming
9. development folder structure
10. compile with user-defined include files and link with user-defined libraries
*Structure is used to store set of values about an object/entity. We sometime want to store multiple such structure variables for hundreds or objects then Array of Structure is used.*
Step 1 Warming-up
Implement a program in a function which gets three student names and ages and store in the structure and print them one by one.
Ask the user to enter a name and his/her age
Use Student array of structure
Use NSTUDENTS
For example:
(0) John, 20
(1) Han, 30
(2) Dong, 40
Step 2 No magic number NSTUDENTS
In this step, we want to remove the magic number NSTUDENTS. Now you must ask the user to enter the number of students and allocate the memory dynamically for the array of structure to store students' name and age.
Allocate a pointer to an array structure instead of using a fixed size array.
Don't use NSTUDENTS, but use nStudents which user entered.
Use the array notation [ ] to access members of the structure
For example:
(0) Axp, 29
(1) Kbpstep 88
(2) Dntomqcey 9
Step 3 Using pointer -> notation to access members of a structure
In this step, it is the same as step 2 except you use a pointer to access members of the structure instead of the array notation.
Don't use the array notation [], but use -> to access members
Don't forget incrementing the pointer in for loop
Step 4 Using typedef and Generating names and ages randomly
In this step, it is the same as step 3 except you generate names and ages randomly instead of asking them to users.
Use typedef Student *pStudent instead of Student *
For example, use pStudent list; don't use Student *list
To use this typedef structure, you must turn off the struct declaration and turn on typedef struct on in the source file provided.
///////////////////////////// typedef struct /////////////////////////////
typedef struct {
char name[BUFFER_SIZE];
int age;
} Student;
typedef Student *pStudent;
Each name begins with a capital letter and its length must be one or more but less than MAX_NAMELENGTH = 10. Implement this functionality in void generateName(char *name)
The age must be between 0 and MAX_AGE = 100. Implement this functionality in void generateAge(int *age)
For example:
(0) Axp, 29
(1) Kbpstep 88
(2) Dntomqcey 9
Step 5 implementing printStructure()
In this step, it is the same as step 4 except you use printStructure().
Implement a function called printStructure() which takes two arguments as shown below: printStructure(pStudent list, int nStudents) printStruecture() prints the contents of structure and its information about the min/max names' length and ages.
Step 6 Passing and getting a pointer
In this step, it is the same as Step 5, except it does not print but returns the pointer of the student list such that the caller of the function prints if necessary.
In main() function, print the list of students, not in step6() function.
Don't forget freeing the pointer in main().
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started