logo

Structura indicator în C

Un pointer de structură este definit ca indicatorul care indică adresa blocului de memorie care stochează a structura cunoscut sub numele de indicator de structură. Structurile complexe de date, cum ar fi listele legate, arborii, graficele etc. sunt create cu ajutorul indicatorilor de structură. Pointerul de structură spune adresa unei structuri din memorie, îndreptând variabila către variabila de structură.
Exemplu:

C




arbore binar vs bst





// 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;> }>



>

>

În codul de mai sus s este o instanță de punct struct și ptr este indicatorul struct, deoarece stochează adresa punctului struct.

Accesarea membrului de structură cu ajutorul indicatorilor

Există două moduri de a accesa membrii structurii cu ajutorul unui pointer de structură:

  1. Cu ajutorul (*) operatorului asterisc sau indirect și (.) operatorului punct.
  2. Cu ajutorul operatorului săgeată ( -> ).

Mai jos este programul pentru a accesa membrii structurii folosind indicatorul de structură cu ajutorul operatorului punct.

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;> }>

>

>

Ieșire:

1>

Mai jos este programul pentru a accesa membrii structurii folosind indicatorul de structura cu ajutorul operatorului Arrow. În acest program, am creat un Structure Student care conține variabila de structură s. Structura Student are rol_no, nume, ramură și lot.

declarația if-else java

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->rola_nu);> >printf>(>'Enter Name of Student '>);> >scanf>(>'%s'>, &ptr->nume);> >printf>(>'Enter Branch of Student '>);> >scanf>(>'%s'>, &ptr->ramură);> >printf>(>'Enter batch of Student '>);> >scanf>(>'%d'>, &ptr->lot);> >// Displaying details of the student> >printf>(>' Student details are: '>);> >printf>(>'Roll No: %d '>, ptr->rola_nu);> >printf>(>'Name: %s '>, ptr->nume);> >printf>(>'Branch: %s '>, ptr->ramură);> >printf>(>'Batch: %d '>, ptr->lot);> >return> 0;> }>

>

>

Ieșire:

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>