Header Ads

                                                  C Programming -Lecture 9 :Structure



Multiple variables can be declared variables of language such as C group. This is called gupatike structure. We have no one person's name, age, and if you want to work with them to put together convenient.
That is why we created the structure karih
struct [struct_name]
{
  [Structure fields]
};
For example:
struct Person
{
    char name [20];
    int age;
    int salary;
};
The structure includes three field name, age and salary. There is no need to hold three separate variables in this field. They will be together always. However alade way to meet these standards, or can be changed.
Now we have the general structure of a primary (primitive type) can be used as a variable of type.
As it is to declare the variable
Person p1;
[Note: Most of the time we use the C ++ compiler. Therefore, absolutely not trying to use pure C Language lectures.]
Initializing a Structure
Other types of variables when you declare variables, such as the initial value can be set in the structure can be
Person p1 = {?? "Kalam" ??, 38, 60000};
In order to keep the stakacarera fields.
To order ulatapalta text fields with the names of the kinds:
Person p1 = {.age = 38, .name = ?? "Kalam" ??, .salary = 60000};
Field before the dot (.) Will be. If you wish one or more of the value of the field, but there is no difficulty.
If you want to work in different fields structure of the dot (.) Can be used
int current_age;

current_age = p1.age
Assignment
Person p1 = {?? "Kalam" ??, 38, 60000};

Person p2;

p2 = p1; / * Copies the member values ??from p1 into p2. * /
In this way, the other the value of a variable to keep in mind if you have two separate copies of the variables will act as. Changing the value will not change the other the one.
For example p2.age = 56; if you p1.age values ??will not change.
Functions and Structures
The return value of the function parameter, or as any other type of structure can be used as variables.
The program of the karunah
#include "stdio.h"
Structure rules

void print_person (Person ).
{
    printf ( Name, name).
    printf ( Age, age).
    printf ( beton, salary).
}

void main ()
{
    Person p1 = {?? "Kalam" ??, 38, 60000};
    print_person (p1);
}
The structure is sent as the value. Changes in the value of the function of the field do not affect the function or value will not change.
Thus, the structure can be used as reference variable (the name of a variable of type & use as bhariyebalera)
#include "stdio.h"

void test_person (Person & p)
{
    if (age  0)
    {
        page 18.
void main ()
{
    Person {Kalam}.
    test_person ;

    printf ( Age d p1age).
}
Here Age: 18 will thus output.
Arrays of Structure
Array structures can be created and used in the same way as other types
To declare will be written as
Person pn [10];
Now you can use the index as a variable of type, thus:

Structure within a Structure (nested structure)
We want to make a pact to keep the addresses of the person using the nested structure, then we do it together is easy:
struct Person
{
    char name [20].
    integer age.
    int salary.
    struct address.
    {
        int house_no;
        char post [30];
        char district [30];
    };
};
How to use the tracked
Person p1;

p1.age = 34;
p1.address.house_no = 2543;

No comments:

Powered by Blogger.