Struktūras rādītājs ir definēts kā rādītājs, kas norāda uz tā atmiņas bloka adresi, kurā glabājas a struktūra pazīstams kā struktūras rādītājs. Ar struktūras rādītāju palīdzību tiek izveidotas sarežģītas datu struktūras, piemēram, saistītie saraksti, koki, grafiki utt. Struktūras rādītājs norāda struktūras adresi atmiņā, norādot mainīgo uz struktūras mainīgo.
Piemērs:
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;> }> |
>
fibonači secība java
>
Iepriekš minētajā kodā s ir struct point un gadījums ptr ir struktūras rādītājs, jo tas saglabā struktūras punkta adresi.
Piekļuve struktūras dalībniekam, izmantojot rādītājus
Ir divi veidi, kā piekļūt struktūras dalībniekiem, izmantojot struktūras rādītāju:
- Ar (*) zvaigznītes vai netiešā operatora un (.) punktu operatora palīdzību.
- Ar ( -> ) bultiņu operatora palīdzību.
Zemāk ir programma, lai piekļūtu struktūras elementiem, izmantojot struktūras rādītāju ar punktu operatora palīdzību.
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;> }> |
>
>Izvade:
1>
Zemāk ir programma, lai piekļūtu struktūras elementiem, izmantojot struktūras rādītāju ar bultiņas operatora palīdzību. Šajā programmā mēs esam izveidojuši struktūras studentu, kas satur struktūras mainīgo s. Struktūras studentam ir roll_no, nosaukums, filiāle un partija.
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->nosaukums);> >printf>(>'Enter Branch of Student
'>);> >scanf>(>'%s'>, &ptr->filiāle);> >printf>(>'Enter batch of Student
'>);> >scanf>(>'%d'>, &ptr->partija);> >// Displaying details of the student> >printf>(>'
Student details are:
'>);> >printf>(>'Roll No: %d
'>, ptr->roll_no);> >printf>(>'Name: %s
'>, ptr->nosaukums);> >printf>(>'Branch: %s
'>, ptr->filiāle);> >printf>(>'Batch: %d
'>, ptr->partija);> >return> 0;> }> |
>
>
Izvade:
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>