Download presentation
Presentation is loading. Please wait.
1
The Pointers of Structures
CHAPTER 11
2
Declaration of a structure pointer
Each array has a pointer with the same name. Any array of structure also uses a structure pointer. You can declare your structure pointers e.g.: structure date { int day, month, year; }; … struct date today, *ptoday; The “today” is a structure date variable, and “ptoday” is a structure date pointer.
3
Declaration of a structure pointer
e.g: #include <stdio.h> #include <conio.h> struct date { int day, month, year; }; void main() { struct date today,* ptoday; ptoday = &today; printf(“\n Please Enter The Date (DD/MM/YYYY) :”); scanf(“%d/%d/%d”, &(*ptoday).day, &(*ptoday).month, &(*ptoday).year); printf(“\n\n So, the date is (%2d/%2d/%2d)”, (*ptoday).day, (*ptoday).month, (*ptoday).year); getch(); }
4
Declaration of a structure pointer
When you declare a structure pointer, it is a must to assign an address of a structure variable to it. Structure pointers can not be used, if they are not point anywhere (if they do not have any address). e.g.: structure date { int day, month, year; }; void main() { structure date *today; printf(“\n Enter the date (DD/MM/YYYY) :”); scanf(“%d/%d/%d”, &(*today).day, &(*today).month, &(*today).year); printf(“\n The date is (%2d/%2d/%4d) :”, (*today).day, (*today).month, (*today).year); }
5
Declaration of a structure pointer
On the previous example, the structure pointer is declared and no address is assigned to. Therefore, the scanf function of the previous example, will not able to be executed successfully. In this example, the data type that will be read in scanf is known, but the address that will keep the read data is unknown. Also, you can assign the address of any array element (array of structure) to the structure pointer.
6
Structure pointers and Arrays
You can use a structure pointer as a counter with an array of structures. e.g: … struct std_rec { char std_no[7], name[21] ; int mt, final,, quiz; float grd; } c213[35]; … struct std_rec *ptemp; int i=0; ptemp = c213; for (i=0; i<35; i++) { printf(“\n %7s %21s %3d % 3d %3d %6.2f”, (*ptemp+i).std_no, (*ptemp+i).std_name, (*ptemp+i).mt, (*ptemp+i).fnal, (*ptemp+i).quiz,); }
7
#include <stdio.h>
#include <conio.h> struct std_rec { char stdno[7], name[20]; int mt, final, quiz; float grd; } c213[3]; void main() { struct std_rec *ptr; int i; char blank[2]; ptr = c213; for (i=0; i<3; i++) printf("\n Enter the student no :"); gets((*ptr).stdno); printf("\n Enter the student name :"); gets((*ptr).name); printf("\n Enter the Midterm score:"); scanf("%d",&((*ptr).mt)); printf("\n Enter the Final score:"); scanf("%d",&((*ptr).final)); printf("\n Enter the Quiz score:"); scanf("%d",&((*ptr).quiz)); gets(blank); (*ptr).grd = 0.3 *(*ptr).mt * (*ptr).final * (*ptr).quiz; printf("%6.2f",(*ptr).grd); } getch();
8
Structure pointers and Functions
A structure pointer can be used as an argument of a function. Also, a structure pointer can be used to keep the return value of a function if it is an address of a structure variable. In a function call, the use of a structure pointer is decreasing the memory requirement of your program (only a local structure pointer is going to be declared at the function side, that needs 4 bytes). In the case, that a structure variable is used as an argument, on the function side, a local structure variable has to be declared (it will require so many bytes).
9
Example #include <stdio.h> #include <conio.h>
struct std_rec { char stdno[7], name[20]; int mt, final, quiz; float grd; } c213[3]; void read_std(struct std_rec *ptr) { char blank[2]; clrscr(); printf("\n Enter the student no :"); gets((*ptr).stdno); printf("\n Enter the student name :"); gets((*ptr).name); printf("\n Enter the Midterm score:"); scanf("%d",&((*ptr).mt)); printf("\n Enter the Final score:"); scanf("%d",&((*ptr).final)); printf("\n Enter the Quiz score:"); scanf("%d",&((*ptr).quiz)); gets(blank); (*ptr).grd = 0.3 *(*ptr).mt * (*ptr).final * (*ptr).quiz; }
10
Example void main() { int i; for (i=0; i<3; i++)
read_std(&c213[i]); printf("GRD: %d6.2f",c213[i].grd); getch(); }
11
Structure pointers and Dynamic Arrays
The array of structures can be declared dynamically. e.g.: struct emp_rec { char emp_no[10], name[10], surname[10]; char status; int hworked; double wage; }; void main() { struct em_rec *workers; int num; printf(“\n How many workers are employed in your company? “); scanf(“%d”,&num); workers = (struct emp_rec *) malloc(num * sizeof(struct emp_rec);_ … }
12
Example 1 #include <stdio.h> #include <conio.h>
#include <alloc.h> struct std_rec { char stdno[7], name[20]; int mt, final, quiz; float grd; } c213[3]; void main() { struct std_rec *c213; int i, num; char blank[2]; printf("\n How many students are there in the class ? "); scanf("%d",&num); c213 = (struct std_rec *) malloc( num * sizeof(struct std_rec));
13
Example 1 for (i=0; i<num; i++) { gets(blank);
printf("\n Enter the student no :"); gets((*(c213+i)).stdno); printf("\n Enter the student name :"); gets((*(c213+i)).name); printf("\n Enter the Midterm score:"); scanf("%d",&(*(c213+i)).mt ); printf("\n Enter the Final score:"); scanf("%d",&(*(c213+i)).final ); printf("\n Enter the Quiz score:"); scanf("%d",&(*(c213+i)).quiz ); (*(c213+i)).grd = 0.3 * (*(c213+i)).mt * (*(c213+i)).final + 0.2 * (*(c213+i)).quiz; printf("%6.2f",(*(c213+i)).grd); } getch();
14
Example 2 #include <stdio.h> #include <conio.h>
#include <alloc.h> struct std_rec { char stdno[7], name[20]; int mt, final, quiz; float grd; } c213[3]; void read_std(struct std_rec *ptr) { char blank[2]; clrscr(); gets(blank); printf("\n Enter the student no :"); gets((*ptr).stdno); printf("\n Enter the student name :"); gets((*ptr).name); printf("\n Enter the Midterm score:"); scanf("%d",&((*ptr).mt)); printf("\n Enter the Final score:"); scanf("%d",&((*ptr).final)); printf("\n Enter the Quiz score:"); scanf("%d",&((*ptr).quiz)); (*ptr).grd = 0.3 *(*ptr).mt * (*ptr).final * (*ptr).quiz; }
15
Example 2 void main() { struct std_rec *c213; int i, num;
char blank[2]; printf("\n How many students are there in the class ? "); scanf("%d",&num); c213 = (struct std_rec *) malloc( num * sizeof(struct std_rec)); for (i=0; i<num; i++) read_std(c213+i); printf("GRD :%6.2f",(*(c213+i)).grd); getch(); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.