Kazalec strukture v C
Kazalec strukture je definiran kot kazalec, ki kaže na naslov pomnilniškega bloka, ki shranjuje a struktura znan kot kazalec strukture. Kompleksne podatkovne strukture, kot so povezani seznami, drevesa, grafi itd., so ustvarjene s pomočjo strukturnih kazalcev. Kazalec strukture pove naslov strukture v pomnilniku tako, da kaže spremenljivko na spremenljivko strukture.
primer:
C
// C program to demonstrate structure pointer> #include> struct> point {> > int> value;> };> int> main()> {> > struct> point s;> > > // Initialization of the structure pointer> > struct> point* ptr = &s;> > return> 0;> }> |
V zgornji kodi s je primerek struct point in ptr je kazalec strukture, ker shranjuje naslov točke strukture.
Dostop do člena strukture s pomočjo kazalcev
Obstajata dva načina za dostop do članov strukture s pomočjo kazalca strukture:
- S pomočjo (*) zvezdice ali posrednega operatorja in (.) operatorja pike.
- S pomočjo ( -> ) operatorja puščice.
Spodaj je program za dostop do členov strukture s pomočjo kazalca strukture s pomočjo operatorja pike.
C
// C Program to demonstrate Structure pointer> #include> #include> struct> Student {> > int> roll_no;> > char> name[30];> > char> branch[40];> > int> batch;> };> int> main()> {> > struct> Student s1;> > struct> Student* ptr = &s1;> > s1.roll_no = 27;> > strcpy> (s1.name,> 'Kamlesh Joshi'> );> > strcpy> (s1.branch,> 'Computer Science And Engineering'> );> > s1.batch = 2019;> > printf> (> 'Roll Number: %d
'> , (*ptr).roll_no);> > printf> (> 'Name: %s
'> , (*ptr).name);> > printf> (> 'Branch: %s
'> , (*ptr).branch);> > printf> (> 'Batch: %d'> , (*ptr).batch);> > return> 0;> }> |
Izhod:
1
Spodaj je program za dostop do členov strukture s kazalcem strukture s pomočjo operatorja Arrow. V tem programu smo ustvarili Structure Student, ki vsebuje strukturno spremenljivko s. Structure Student ima roll_no, ime, podružnico in serijo.
C
// C Program to demonstrate Structure pointer> #include> #include> // Creating Structure Student> struct> Student {> > int> roll_no;> > char> name[30];> > char> branch[40];> > int> batch;> };> // variable of structure with pointer defined> struct> Student s, *ptr;> int> main()> {> > ptr = &s;> > // Taking inputs> > printf> (> 'Enter the Roll Number of Student
'> );> > scanf> (> '%d'> , &ptr->roll_no);> > printf> (> 'Enter Name of Student
'> );> > scanf> (> '%s'> , &ptr->ime);> > printf> (> 'Enter Branch of Student
'> );> > scanf> (> '%s'> , &ptr->podružnica);> > printf> (> 'Enter batch of Student
'> );> > scanf> (> '%d'> , &ptr->serija);> > // Displaying details of the student> > printf> (> '
Student details are:
'> );> > printf> (> 'Roll No: %d
'> , ptr->roll_no);> > printf> (> 'Name: %s
'> , ptr->ime);> > printf> (> 'Branch: %s
'> , ptr->podružnica);> > printf> (> 'Batch: %d
'> , ptr->serija);> > return> 0;> }> |
Izhod:
Enter the Roll Number of Student 27 Enter Name of Student Kamlesh_Joshi Enter Branch of Student Computer_Science_And_Engineering Enter batch of Student 2019 Student details are: Roll No: 27 Name: Kamlesh_Joshi Branch: Computer_Science_And_Engineering Batch: 2019