Sunday, 27 March 2016

Structure


Defination :-

                          Structure is a user defined data type. struct keyword is used to define a structure. Structure  is a collection of different data types elements under a single name and define a structure to hold this information.

                      structure and array are similar the only difference is that array is used to store collection of similar data types while structure can store collection of any type of data

Structure of structure name, variable can be declared as:-

 
struct structure_name
{
    char variable1[100];
    int variable2;
    float variable3;
};

Structure variable declaration


struct structure_name
{
    char variable1[100];
    int variable2;
    float variable3;
}v1 ,v2 ,v[20];

Accessing members:-

  1. Member operator(.)
  2. Structure pointer operator(->)

Program:-

 
#include <stdio.h>
#include <string.h>
struct student
{
        int id;
        char name[30];
        float percentage;
};

int main()
{
    struct student record1 = {1, "paidi", 90.5};
    printf("Records of STUDENT1: \n");
    printf("  Id is: %d \n", record1.id);
    printf("  Name is: %s \n", record1.name);
    printf("  Percentage is: %f \n\n", record1.percentage);
    return 0;
}
printf("struct size------->= %d\n",a);
}

typedef while using structure:


typedef struct structure_name
{
int a;
int b;
int c;
}cov;

by using this typedef we can give number of declare variable of students.

Structures within structures:-


Structures can be nested within other structures.

struct STR
{
int value;
float value1;
};

struct number
{
   struct STR nested1;
   int value2;
}v1,v2;


you want to access value for v2 structure variable then, structure member v1.nested1.value is used
 
#include <stdio.h>
#include <string.h>
struct student_college_detail
{
    int college_id;
    char college_name[50];
};
struct student_detail
{
   int id;
   char name[20];
   float percentage;
  // structure within structure
   struct student_college_detail clg_data;
}stu_data;
int main()
{
  struct student_detail stu_data = {1, "Ramana", 90.5, 71145,"Globalin institute"};
    printf(" Id is: %d \n", stu_data.id);
    printf(" Name is: %s \n", stu_data.name);
    printf(" Percentage is: %f \n\n", stu_data.percentage);
    printf(" College Id is: %d \n", stu_data.clg_data.college_id);
    printf(" College Name is: %s \n",stu_data.clg_data.college_name);
    return 0;
}

Structure Programs :-

 
1. Array using Struct :

#include <stdio.h>
#include <string.h>
struct student
{
     int id;
     char name[30];
     float percentage;
};
struct student record[2];
int main()
{
     int i;
     // 1st student's record
     record[0].id=1;
     strcpy(record[0].name, "Rajana");
     record[0].percentage = 86.5;
     // 2nd student's record        
     record[1].id=2;
     strcpy(record[1].name, "kumar");
     record[1].percentage = 90.5;
     // 3rd student's record
     record[2].id=3;
     strcpy(record[2].name, "vadar");
     record[2].percentage = 81.5;

     for(i=0; i<3; i++)
     {
         printf("Records of STUDENT : %d \n", i+1);
         printf(" Id is: %d \n", record[i].id);
         printf(" Name is: %s \n", record[i].name);
         printf(" Percentage is: %f\n\n",record[i].percentage);
     }
     return 0;
}
2. Function Using Structure:
 
#include <stdio.h>
#include <string.h>
struct student
{
   int id;
   char name[20];
   float percentage;
};
void func(struct student record);
int main()
{
  struct student record;
  record.id=1;
  strcpy(record.name, "Raju");
  record.percentage = 86.5;
  func(record);
  return 0;
}
void func(struct student record)
{
  printf(" Id is: %d \n", record.id);
  printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
}
3. Pointer Using Structure:
#include <stdio.h>
#include <string.h>
struct student
{
     int id;
     char name[30];
     float percentage;
};
struct student record1 = {1, "Raju", 90.5};
int main()
{
     int i;
     struct student *ptr;
     ptr = &record1;    
     printf("Records of STUDENT1: \n");
     printf("  Id is: %d \n", ptr->id);
     printf("  Name is: %s \n", ptr->name);
     printf("  Percentage is: %f \n\n", ptr->percentage);
     return 0;
}

4. pointer using Nested Structure:

#include <stdio.h>
#include <string.h>
struct student_college_detail
{
    int college_id;
    char college_name[50];
};
struct student_detail
{
    int id;
    char name[20];
    float percentage;
    // structure within structure
    struct student_college_detail clg_data;
}stu_data, *stu_data_ptr;
int main()
{
  struct student_detail stu_data = {1,"Rajana",90.5,71145,"Network    institute"};
  stu_data_ptr = &stu_data;
  printf(" Id is:%d\n",stu_data_ptr->id);
  printf(" Name is:%s\n",stu_data_ptr->name);
  printf(" Percentage is:%f\n\n",stu_data_ptr->percentage);
  printf("Collgid is:%d\n",stu_data_ptr->clg_data.college_id);
  printf("CollgNameis:%s\n",stu_data_ptr->clg_data.college_name);
}
 
5. example program of structure inside union:
 
#include <stdio.h>
#include <string.h>
struct student_college_detail
{
    int college_id;
    char college_name[50];
};
union student_detail
{
    int id;
    char name[40];
    int num;
    struct student_college_detail clg_data;   // structure within structure
}stu_data, *ptr;
int main()
{
    union student_detail stu_data;
    ptr = &stu_data;
    ptr->id = 2;
    printf(" Id is: %d \n", ptr->id);
    strcpy(ptr->name,"Rajana");
    printf(" Name is: %s \n",ptr->name);
    ptr->num = 9;
    printf(" num is: %d \n\n", ptr->num);
    printf(" Id is: %d \n", ptr->id);
    printf(" Name is: %s \n",ptr->name);
    printf(" num is: %d \n\n", ptr->num);
}
-----------------------------------------------------------------------------------------
More details about C interview questions and programs: http://learnclanguage4.blogspot.com/

No comments:

Post a Comment

Structure

Defination :-                           Structure is a user defined data type. struct keyword is used to define a structure. Structur...