Ukazatel struktury v C

Ukazatel struktury je definován jako ukazatel, který ukazuje na adresu paměťového bloku, který ukládá a struktura známý jako ukazatel struktury. Složité datové struktury jako Propojené seznamy, stromy, grafy atd. jsou vytvářeny pomocí ukazatelů struktury. Ukazatel struktury sděluje adresu struktury v paměti tím, že ukazuje proměnnou na proměnnou struktury.
Příklad:

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

Ve výše uvedeném kódu s je instancí třídy struct point and ptr je ukazatel struktury, protože ukládá adresu bodu struktury.

Přístup ke členu struktury pomocí ukazatelů

Existují dva způsoby přístupu k členům struktury pomocí ukazatele struktury:

  1. S pomocí (*) hvězdička nebo nepřímý operátor a (.) tečkový operátor.
  2. S pomocí ( -> ) šipkového operátoru.

Níže je uveden program pro přístup ke členům struktury pomocí ukazatele struktury s pomocí operátoru tečka.

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

Výstup:

1 

Níže je uveden program pro přístup ke členům struktury pomocí ukazatele struktury s pomocí operátoru Šipka. V tomto programu jsme vytvořili Structure Student obsahující strukturní proměnnou s. Structure Student má číslo role, název, větev a dávku.

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->jméno);> > printf> (> 'Enter Branch of Student '> );> > scanf> (> '%s'> , &ptr->větev);> > printf> (> 'Enter batch of Student '> );> > scanf> (> '%d'> , &ptr->dávka);> > // Displaying details of the student> > printf> (> ' Student details are: '> );> > printf> (> 'Roll No: %d '> , ptr->roll_no);> > printf> (> 'Name: %s '> , ptr->jméno);> > printf> (> 'Branch: %s '> , ptr->větev);> > printf> (> 'Batch: %d '> , ptr->dávka);> > return> 0;> }>

Výstup:

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