logo

Struktūros rodyklė C

Struktūros rodyklė apibrėžiama kaip žymeklis, nukreipiantis į atminties bloko, kuriame saugoma a, adresą struktūra žinomas kaip struktūros rodyklė. Sudėtingos duomenų struktūros, tokios kaip susieti sąrašai, medžiai, grafikai ir kt., sukuriamos struktūrinių rodyklių pagalba. Struktūros žymeklis nurodo struktūros adresą atmintyje, nukreipdamas kintamąjį į struktūros kintamąjį.
Pavyzdys:

C






panaši sąsaja „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;> }>

>

>

Aukščiau pateiktame kode s yra struktūros taško ir pavyzdys ptr yra struktūros rodyklė, nes ji saugo struct taško adresą.

Prieiga prie struktūros nario rodyklių pagalba

Yra du būdai pasiekti struktūros narius naudojant struktūros žymeklį:

kurie baigė mokyklą
  1. Naudojant (*) žvaigždutę arba netiesioginį operatorių ir (.) taško operatorių.
  2. Su ( -> ) rodyklės operatoriumi.

Žemiau yra programa, skirta pasiekti struktūros elementus naudojant struktūros žymeklį, naudojant taško operatorių.

C


nelygus sumavimas



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

>

>

išvalykite npm talpyklą
Išvestis:

1>

Žemiau yra programa, skirta pasiekti struktūros narius naudojant struktūros žymeklį naudojant rodyklės operatorių. Šioje programoje sukūrėme struktūros mokinį, kuriame yra struktūros kintamasis s. Struktūros studentas turi roll_no, pavadinimą, šaką ir partiją.

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->vardas);> >printf>(>'Enter Branch of Student '>);> >scanf>(>'%s'>, &ptr->filialas);> >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->vardas);> >printf>(>'Branch: %s '>, ptr->filialas);> >printf>(>'Batch: %d '>, ptr->partija);> >return> 0;> }>

sujungimas java eilutė

>

>

Išvestis:

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>