Saturday, 8 February 2014

C – Structure using Pointer


C – Structure using Pointer

      Prev                                                                                              Next

C structure can be accessed in 2 ways in a C program. They are,
    1. Using normal structure variable
    2. Using pointer variable

       Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using pointer variable. You have learnt how to access structure data using normal variable in C – Structure topic. So, we are showing here how to access structure data using pointer variable in below C program.

Example program for C structure using pointer:

           In this program, “record1″ is normal structure variable and “ptr” is pointer structure variable. As you know, Dot(.) operator is used to access the data using normal structure variable and arrow(->) is used to access data using pointer variable.

Output:

Records of STUDENT1:
Id is: 1
Name is: Raju
Percentage is: 90.500000

Example program to copy a structure in C:

There are many methods to copy one structure to another structure in C.
    1. We can copy using direct assignment of one structure to another structure or
    2. we can use C inbuilt function “memcpy()” or
    3. we can copy by individual structure members.

Output:

Records of STUDENT1 - record1 structure
Id : 1
Name : Raju
Percentage : 90.500000  Records of STUDENT1 – Direct copy from record1
Id : 1
Name : Raju
Percentage : 90.500000
Records of STUDENT1 – copied from record1 using memcpy
Id : 1
Name : Raju
Percentage : 90.500000
Records of STUDENT1 – Copied individual members from record1
Id : 1
Name : Raju
Percentage : 90.500000


Passing struct to function in c


         Passing struct to function

      Prev                                                                                               Next

  • A structure can be passed to any function from main function or from any sub function.
  • Structure definition will be available within the function only.
  • It won’t be available to other functions unless it is passed to those functions by value or by address(reference).
  • Else, we have to declare structure variable as global variable. That means, structure variable should be declared outside the main function. So, this structure will be visible to all the functions in a C program.

Passing structure to function in C:

It can be done in below 3 ways.
    1. Passing structure to a function by value
    2. Passing structure to a function by address(reference)
    3. No need to pass a structure – Declare structure variable as global

Example program – passing structure to function in C by value:

           In this program, the whole structure is passed to another function by value. It means the whole structure is passed to another function with all members and their values. So, this structure can be accessed from called function. This concept is very useful while writing very big programs in C.

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Example program – Passing structure to function in C by address:

           In this program, the whole structure is passed to another function by address. It means only the address of the structure is passed to another function. The whole structure is not passed to another function with all members and their values. So, this structure can be accessed from called function by its address.

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000

Example program to declare a structure variable as global in C:

           Structure variables also can be declared as global variables as we declare other variables in C. So, When a structure variable is declared as global, then it is visible to all the functions in a program. In this scenario, we don’t need to pass the structure to any function separately.

Output:

Id is: 1
Name is: Raju
Percentage is: 86.500000
……


      Prev                                                                                               Next

.


Array of Structures in c


C – Array of Structures

      Prev                                                                                               Next

           As you know, C Structure is collection of different datatypes ( variables ) which are grouped together. Whereas, array of structures is nothing but collection of structures. This is also called as structure array in C.
……

Example program for array of structures in C:

           This program is used to store and access “id, name and percentage” for 3 students. Structure array is used in this program to store and display records for many students. You can store “n” number of students record by declaring structure variable as ‘struct student record[n]“, where n can be 1000 or 5000 etc.

Output:

Records of STUDENT : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000
Records of STUDENT : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Records of STUDENT : 3
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000

Example program for declaring many structure variable in C:

           In this program, two structure variables “record1″ and “record2″ are declared for same structure and different values are assigned for both structure variables. Separate memory is allocated for both structure variables to store the data.

Output:

Records of STUDENT1:
Id is: 1
Name is: Raju
Percentage is: 90.500000
Records of STUDENT2:
Id is: 2
Name is: Mani
Percentage is: 93.500000


      Prev                                                                                               Next

.