Zakaj uporabljati strukturo?
V C obstajajo primeri, ko moramo shraniti več atributov entitete. Ni nujno, da ima subjekt vse informacije samo ene vrste. Lahko ima različne atribute različnih vrst podatkov. Na primer, entiteta študent ima lahko svoje ime (string), številko zvitka (int), oznake (float). Za shranjevanje takšne vrste informacij o študentu entitete imamo naslednje pristope:
- Sestavite posamezne nize za shranjevanje imen, številk zvitkov in oznak.
- Uporabite posebno podatkovno strukturo za shranjevanje zbirke različnih tipov podatkov.
Oglejmo si podrobneje prvi pristop.
#include void main () { char names[2][10],dummy; // 2-dimensioanal character array names is used to store the names of the students int roll_numbers[2],i; float marks[2]; for (i=0;i<3;i++) { printf('enter the name, roll number, and marks of student %d',i+1); scanf('%s %d %f',&names[i],&roll_numbers[i],&marks[i]); scanf('%c',&dummy); enter will be stored into dummy character at each iteration } printf('printing details ... '); for (i="0;i<3;i++)" printf('%s %f ',names[i],roll_numbers[i],marks[i]); < pre> <p> <strong>Output</strong> </p> <pre> Enter the name, roll number, and marks of the student 1Arun 90 91 Enter the name, roll number, and marks of the student 2Varun 91 56 Enter the name, roll number, and marks of the student 3Sham 89 69 Printing the Student details... Arun 90 91.000000 Varun 91 56.000000 Sham 89 69.000000 </pre> <p>The above program may fulfill our requirement of storing the information of an entity student. However, the program is very complex, and the complexity increase with the amount of the input. The elements of each of the array are stored contiguously, but all the arrays may not be stored contiguously in the memory. C provides you with an additional and simpler approach where you can use a special data structure, i.e., structure, in which, you can group all the information of different data type regarding an entity.</p> <h2>What is Structure</h2> <p>Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member. Structures ca; simulate the use of classes and templates as it can store various information </p> <p>The <strong>,struct</strong> keyword is used to define the structure. Let's see the syntax to define the structure in c.</p> <pre> struct structure_name { data_type member1; data_type member2; . . data_type memeberN; }; </pre> <p>Let's see the example to define a structure for an entity employee in c.</p> <pre> struct employee { int id; char name[20]; float salary; }; </pre> <p>The following image shows the memory allocation of the structure employee that is defined in the above example.</p> <img src="//techcodeview.com/img/c-tutorial/01/c-structure.webp" alt="c structure memory allocation"> <p>Here, <strong>struct</strong> is the keyword; <strong>employee</strong> is the name of the structure; <strong>id</strong> , <strong>name</strong> , and <strong>salary</strong> are the members or fields of the structure. Let's understand it by the diagram given below:</p> <img src="//techcodeview.com/img/c-tutorial/01/c-structure-2.webp" alt="c structure"> <h2>Declaring structure variable</h2> <p>We can declare a variable for the structure so that we can access the member of the structure easily. There are two ways to declare structure variable:</p> <ol class="points"> <li>By struct keyword within main() function</li> <li>By declaring a variable at the time of defining the structure.</li> </ol> <p> <strong>1st way:</strong> </p> <p>Let's see the example to declare the structure variable by struct keyword. It should be declared within the main function.</p> <pre> struct employee { int id; char name[50]; float salary; }; </pre> <p>Now write given code inside the main() function.</p> <pre> struct employee e1, e2; </pre> <p>The variables e1 and e2 can be used to access the values stored in the structure. Here, e1 and e2 can be treated in the same way as the objects in <a href="/c-tutorial">C++</a> and <a href="/java-tutorial">Java</a> .</p> <p> <strong>2nd way:</strong> </p> <p>Let's see another way to declare variable at the time of defining the structure.</p> <pre> struct employee { int id; char name[50]; float salary; }e1,e2; </pre> <h3>Which approach is good</h3> <p>If number of variables are not fixed, use the 1st approach. It provides you the flexibility to declare the structure variable many times.</p> <p>If no. of variables are fixed, use 2nd approach. It saves your code to declare a variable in main() function.</p> <h2>Accessing members of the structure</h2> <p>There are two ways to access structure members:</p> <ol class="points"> <li>By . (member or dot operator)</li> <li>By -> (structure pointer operator)</li> </ol> <p>Let's see the code to access the <em>id</em> member of <em>p1</em> variable by. (member) operator.</p> <pre> p1.id </pre> <h3>C Structure example</h3> <p>Let's see a simple example of structure in C language.</p> <pre> #include #include struct employee { int id; char name[50]; }e1; //declaring e1 variable for structure int main( ) { //store first employee information e1.id=101; strcpy(e1.name, 'Sonoo Jaiswal');//copying string into char array //printing first employee information printf( 'employee 1 id : %d ', e1.id); printf( 'employee 1 name : %s ', e1.name); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> employee 1 id : 101 employee 1 name : Sonoo Jaiswal </pre> <p>Let's see another example of the structure in <a href="/c-programming-language-tutorial">C language</a> to store many employees information.</p> <pre> #include #include struct employee { int id; char name[50]; float salary; }e1,e2; //declaring e1 and e2 variables for structure int main( ) { //store first employee information e1.id=101; strcpy(e1.name, 'Sonoo Jaiswal');//copying string into char array e1.salary=56000; //store second employee information e2.id=102; strcpy(e2.name, 'James Bond'); e2.salary=126000; //printing first employee information printf( 'employee 1 id : %d ', e1.id); printf( 'employee 1 name : %s ', e1.name); printf( 'employee 1 salary : %f ', e1.salary); //printing second employee information printf( 'employee 2 id : %d ', e2.id); printf( 'employee 2 name : %s ', e2.name); printf( 'employee 2 salary : %f ', e2.salary); return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> employee 1 id : 101 employee 1 name : Sonoo Jaiswal employee 1 salary : 56000.000000 employee 2 id : 102 employee 2 name : James Bond employee 2 salary : 126000.000000 </pre> <hr></3;i++)>
Zgornji program lahko izpolni našo zahtevo po shranjevanju podatkov študenta entitete. Vendar je program zelo zapleten in zapletenost narašča s količino vnosa. Elementi vsake matrike so shranjeni zvezno, vendar vse matrike morda ne bodo shranjene zvezno v pomnilniku. C vam ponuja dodaten in enostavnejši pristop, kjer lahko uporabite posebno podatkovno strukturo, tj. strukturo, v kateri lahko združujete vse informacije različnih podatkovnih tipov o entiteti.
Kaj je Struktura
Struktura v c je uporabniško definiran podatkovni tip, ki nam omogoča shranjevanje zbirke različnih podatkovnih tipov. Vsak element strukture se imenuje člen. Strukture ca; simulira uporabo razredov in predlog, saj lahko shrani različne informacije
'bančnikov algoritem'
The ,struct ključna beseda se uporablja za definiranje strukture. Oglejmo si sintakso za definiranje strukture v c.
struct structure_name { data_type member1; data_type member2; . . data_type memeberN; };
Oglejmo si primer za definiranje strukture za zaposlenega v subjektu v c.
struct employee { int id; char name[20]; float salary; };
Naslednja slika prikazuje dodelitev pomnilnika zaposlenemu v strukturi, ki je definirana v zgornjem primeru.
tukaj, struct je ključna beseda; zaposleni je ime strukture; id , ime , in plača so člani ali polja strukture. Razumejmo ga s spodnjim diagramom:
Deklaracija strukturne spremenljivke
Za strukturo lahko deklariramo spremenljivko, tako da lahko enostavno dostopamo do člana strukture. Obstajata dva načina za deklaracijo strukturne spremenljivke:
vrste zanke for
- S ključno besedo struct znotraj funkcije main().
- Z deklaracijo spremenljivke v času definiranja strukture.
1. način:
Oglejmo si primer deklaracije strukturne spremenljivke s ključno besedo struct. Deklarirati ga je treba znotraj glavne funkcije.
struct employee { int id; char name[50]; float salary; };
Zdaj napišite dano kodo znotraj funkcije main().
struct employee e1, e2;
S spremenljivkama e1 in e2 lahko dostopate do vrednosti, shranjenih v strukturi. Tukaj lahko e1 in e2 obravnavamo na enak način kot predmete v C++ in Java .
2. način:
java delaj medtem
Oglejmo si še en način za deklaracijo spremenljivke v času definiranja strukture.
struct employee { int id; char name[50]; float salary; }e1,e2;
Kateri pristop je dober
Če število spremenljivk ni določeno, uporabite 1. pristop. Zagotavlja vam prilagodljivost, da strukturno spremenljivko večkrat deklarirate.
Če št. spremenljivk fiksnih, uporabite 2. pristop. Shrani vašo kodo za deklaracijo spremenljivke v funkciji main().
drsenje miške ne deluje
Dostopanje do članov strukture
Obstajata dva načina za dostop do članov strukture:
- avtor . (član ali operator pike)
- Avtor -> (operator kazalca strukture)
Oglejmo si kodo za dostop do id član p1 spremenljivka po. (član) operater.
p1.id
C Primer strukture
Oglejmo si preprost primer strukture v jeziku C.
#include #include struct employee { int id; char name[50]; }e1; //declaring e1 variable for structure int main( ) { //store first employee information e1.id=101; strcpy(e1.name, 'Sonoo Jaiswal');//copying string into char array //printing first employee information printf( 'employee 1 id : %d ', e1.id); printf( 'employee 1 name : %s ', e1.name); return 0; }
Izhod:
employee 1 id : 101 employee 1 name : Sonoo Jaiswal
Oglejmo si še en primer strukture v jezik C za shranjevanje informacij o številnih zaposlenih.
#include #include struct employee { int id; char name[50]; float salary; }e1,e2; //declaring e1 and e2 variables for structure int main( ) { //store first employee information e1.id=101; strcpy(e1.name, 'Sonoo Jaiswal');//copying string into char array e1.salary=56000; //store second employee information e2.id=102; strcpy(e2.name, 'James Bond'); e2.salary=126000; //printing first employee information printf( 'employee 1 id : %d ', e1.id); printf( 'employee 1 name : %s ', e1.name); printf( 'employee 1 salary : %f ', e1.salary); //printing second employee information printf( 'employee 2 id : %d ', e2.id); printf( 'employee 2 name : %s ', e2.name); printf( 'employee 2 salary : %f ', e2.salary); return 0; }
Izhod:
employee 1 id : 101 employee 1 name : Sonoo Jaiswal employee 1 salary : 56000.000000 employee 2 id : 102 employee 2 name : James Bond employee 2 salary : 126000.000000
3;i++)>