Покажчик структури в C

Покажчик структури визначається як покажчик, який вказує на адресу блоку пам'яті, який зберігає a структура відомий як покажчик структури. Складні структури даних, такі як пов’язані списки, дерева, графіки тощо, створюються за допомогою покажчиків на структуру. Покажчик структури повідомляє адресу структури в пам'яті, вказуючи змінну на структурну змінну.
приклад:

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

У наведеному вище коді с є екземпляром struct point і птр є покажчиком структури, оскільки він зберігає адресу точки структури.

Доступ до члена структури за допомогою покажчиків

Існує два способи доступу до членів структури за допомогою покажчика структури:

  1. За допомогою (*) зірочки або оператора непрямості та (.) оператора крапки.
  2. За допомогою оператора стрілки ( -> ).

Нижче наведено програму для доступу до елементів структури за допомогою покажчика структури за допомогою оператора крапки.

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

Вихід:

1 

Нижче наведено програму для доступу до елементів структури за допомогою покажчика структури за допомогою оператора Arrow. У цій програмі ми створили Structure Student, що містить структурну змінну s. Structure Student має roll_no, назву, гілку та партію.

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->ім'я);> > printf> (> 'Enter Branch of Student '> );> > scanf> (> '%s'> , &ptr->відділення);> > printf> (> 'Enter batch of Student '> );> > scanf> (> '%d'> , &ptr->партія);> > // Displaying details of the student> > printf> (> ' Student details are: '> );> > printf> (> 'Roll No: %d '> , ptr->roll_no);> > printf> (> 'Name: %s '> , ptr->ім'я);> > printf> (> 'Branch: %s '> , ptr->відділення);> > printf> (> 'Batch: %d '> , ptr->партія);> > return> 0;> }>

Вихід:

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