Un puntatore a struttura è definito come il puntatore che punta all'indirizzo del blocco di memoria che memorizza a struttura noto come puntatore di struttura. Strutture dati complesse come elenchi collegati, alberi, grafici, ecc. vengono create con l'aiuto di puntatori a struttura. Il puntatore della struttura indica l'indirizzo di una struttura in memoria puntando la variabile alla variabile della struttura.
Esempio:
C
albero binario 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;> }> |
>
>
Nel codice sopra S è un'istanza di struct point e ptr è il puntatore alla struttura perché memorizza l'indirizzo del punto di struttura.
Accesso al membro della struttura con l'aiuto dei puntatori
Esistono due modi per accedere ai membri della struttura con l'aiuto di un puntatore a struttura:
- Con l'aiuto dell'asterisco (*) o dell'operatore indiretto e dell'operatore punto (.).
- Con l'aiuto dell'operatore Freccia ( -> ).
Di seguito è riportato il programma per accedere ai membri della struttura utilizzando il puntatore della struttura con l'aiuto dell'operatore punto.
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;> }> |
>
allineamento img css
>Produzione:
1>
Di seguito è riportato il programma per accedere ai membri della struttura utilizzando il puntatore della struttura con l'aiuto dell'operatore Freccia. In questo programma, abbiamo creato una struttura Student contenente la variabile di struttura s. Lo Structure Student ha roll_no, nome, ramo e batch.
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->nome);> >printf>(>'Enter Branch of Student
'>);> >scanf>(>'%s'>, &ptr->ramo);> >printf>(>'Enter batch of Student
'>);> >scanf>(>'%d'>, &ptr->batch);> >// Displaying details of the student> >printf>(>'
Student details are:
'>);> >printf>(>'Roll No: %d
'>, ptr->roll_no);> >printf>(>'Name: %s
'>, ptr->nome);> >printf>(>'Branch: %s
'>, ptr->ramo);> >printf>(>'Batch: %d
'>, ptr->batch);> >return> 0;> }> |
>
>
tabella desc in mysql
Produzione:
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>