Wskaźnik struktury definiuje się jako wskaźnik wskazujący adres bloku pamięci przechowującego a Struktura znany jako wskaźnik struktury. Złożone struktury danych, takie jak listy połączone, drzewa, wykresy itp. są tworzone za pomocą wskaźników struktur. Wskaźnik struktury podaje adres struktury w pamięci, wskazując zmienną na zmienną struktury.
Przykład:
C
matematyka losowa Java
// 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;> }> |
>
>
W powyższym kodzie S jest instancją punktu struktury i pt jest wskaźnikiem struktury, ponieważ przechowuje adres punktu struktury.
Dostęp do elementu struktury za pomocą wskaźników
Istnieją dwa sposoby uzyskania dostępu do elementów struktury za pomocą wskaźnika struktury:
- Za pomocą (*) gwiazdki lub operatora pośredniego i (.) operatora kropki.
- Za pomocą ( -> ) operatora strzałki.
Poniżej znajduje się program umożliwiający dostęp do elementów konstrukcji za pomocą wskaźnika struktury i operatora kropki.
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;> }> |
najpiękniejszy uśmiech na świecie
>
>Wyjście:
1>
Poniżej znajduje się program umożliwiający dostęp do elementów konstrukcji za pomocą wskaźnika struktury i operatora Arrow. W tym programie utworzyliśmy Studenta Struktury zawierającego zmienne struktury s. Struktura Student ma roll_no, nazwę, gałąź i partię.
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->imię);> >printf>(>'Enter Branch of Student
'>);> >scanf>(>'%s'>, &ptr->oddział);> >printf>(>'Enter batch of Student
'>);> >scanf>(>'%d'>, &ptr->partia);> >// Displaying details of the student> >printf>(>'
Student details are:
'>);> >printf>(>'Roll No: %d
'>, ptr->roll_no);> >printf>(>'Name: %s
'>, ptr->imię);> >printf>(>'Branch: %s
'>, ptr->oddział);> >printf>(>'Batch: %d
'>, ptr->partia);> >return> 0;> }> |
>
>
Wyjście:
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>