Header Ads

Struct:C Programming Language - Part 8

Struct



We know about data types, int, char, float etc. With Structures we can create data structures like ourselves. Such an array is a data structure. Where only we can keep the same data type data. But by creating the structures we can put together different data such as int, char, float and so on. Using Structures, we can create data structures like ourselves. And there we can keep one or more different data as we wish.

 Define Structure

Before using the structures, it is to define it. Structure is defined as the following.

1
2
3
4
5
6
7
Struct name
{
    Member 1;
    Member 2;
    ...
    Member n;
};
Structures are defined with struct keywords. Then you have to write its members. What information will the member have? Such as a book named Structure

1
2
3
4
5
Struct book
{
    Int id;
    Char name;
};
The name of our structure is the book, which is the name of our own structure. Then, in the second bracket, the Structure Members

Here is a member of the integer, where we will place the serial number of the book. Another is the curator, where we will name the book.

Structure Declaration
After Defining Structures it has to be used to declare it to be used. To use an integer variable as before, it has to be declarated as well.
To declare the following is to write
Struct book myBook;
Where struct means we are going to declare a structure. Then the book, which means we are going to declare a struct. Next we will use the name of the structures we created.

After declaring, use the structures. So we'll use our built mybook structure.

Two members of myBook One is not another name.

Now we'll set the book's number and name. Write for him

1
2
Mybook.no = 3;
MyBook.name = 'C';
Now we want to print the name of the book set and no

1
2
Printf ("Book No:% dn", myBook.no);
Printf ("Book Name:% cn", myBook.name);
We have written a small code, now write the whole program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
Struct book {
    Int id;
    Char name;
};
Int main ()
{
    Struct book myBook;
    Mybook.id = 3;
    MyBook.name = 'C';
    Printf ("Book ID:% d \ n", myBook.id);
    Printf ("Book Name:% c \ n", myBook.name);
    Return 0;
}
Now you know the question, why do we use a character instead of the book's name. The reason was simply to explain the structure. Now we can use the full name of the book. He has to take a character array. See the following program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
Struct book {
    Int id;
    Char name [80];
};
Int main ()
{
    Struct book myBook;
    Mybook.id = 3;
    Strcpy (myBook.name, "C Programming");
    Printf ("Book ID:% d \ n", myBook.id);
    Printf ("Book Name:% s \ n", myBook.name);
    Return 0;
}
Look here, we could have used the same equals as before, using the same equals sign, I can not do that in the string. In case of strings the strcpy function is to be used. And for the first time this liberalization was not used. Run the above program and see, our structures are working fine.

 Typedef
We use int for integer variables, we can give a structure and a type of structure we have created. It is given through typedef. Using typedef, we can use our structured structures without using struct keywords when creating our instinct's instanc. For that, we have to write the upper stackery as below

1
2
3
4
Typedef struct {
    Int no;
    Char name;
} Book;
The difference is that we have used a new keyword here, typedef. Then write struct And our structures are written at the end of our stackery.

Now we can create our own book structure without having to use struct keywords, such as:
Book myBook;
The way we declare a variable, just like that. The previous program uses typedef:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
Typedef struct book {
    Int no;
    Char name;
} Book;
Int main ()
{
    Book myBook;
    Mybook.no = 3;
    MyBook.name = 'C';
    Printf ("Book No:% d \ n", myBook.no);
    Printf ("Book Name:% c \ n", myBook.name);
    Return 0;
}
Once the defragment is done, we can create the desired instance of the instance after defining it. In the previous two programs, we define a structure, and then declare a single structure. Now we will write another program, where three structures of a structure will be created.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
Typedef struct book {
    Int id;
    Char name;
} Book;
Int main ()
{
    Book book1;
    Book book2;
    Book book3;
    Book1.id = 1;
    Book1.name = 'A';
    Book2.id = 2;
    Book2.name = 'B';
    Book3.id = 3;
    Book3.name = 'C';
    Printf ("Book ID:% d \ n", book1.id);
    Printf ("Book Name:% c \ n \ n", book1.name);
    Printf ("Book ID:% d \ n", book2.id);
    Printf ("Book Name:% c \ n \ n", book2.name);
    Printf ("Book ID:% d \ n", book3.id);
    Printf ("Book Name:% c \ n", book3.name);
    Return 0;
}
When creating the instance of book, we can write in different lines and write in the same line. Such as

Instead we can write book book1, book2, book3;
We've created three instances of the book and have set the data in all the numbers manually. But we have to write programs where we have to create 100 or 1000 or more instances. What to do Do not see any such program, is not it? But no, we can use conditional here. With the loop, we can set the data in all the places. Again, we can print out the data from all the loops.
We will write a simple program for that. Actually this program does not have to use structures. Then using the loop, we will see an example of how to access different members of the structure.
In the program, we will create a chart of 0 to 100 square root.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
Typedef struct squareRoot {
    Int number [100];
    Double root [100];
} SquareRoot;
Int main ()
{
    Int i = 0;
    SquareRoot squareRoot1;
    // setting data
    For (i = 0; i <= 100; i ++) {
        SquareRoot1.number [i] = i;
        SquareRoot1.root [i] = sqrt (i);
    }
    // print data
    For (i = 0; i <= 100; i ++) {
        Printf ("Square Root of% d", squareRoot1.number [i]);
        Printf ("is:% f \ n", squareRoot1.root [i]);
    }
    Return 0;
}
Here sqrt () is a library function. One of which gives a number, returns the square root of that number. The rest of the codes are easy. First we use the for loop to set the data in various square of our squareRoot1. I again printed them with the loop again. These are the main topics of the structure. Now try to write any complex program using your immigrant energy.

Our YouTube Channel:Clik Here


No comments

Theme images by sebastian-julian. Powered by Blogger.