What is Structures in C Programming

STRUCTURES IN C PROGRAMMING

DEFINITION AND INTRODUCTION:

A structure is a user-defined datatype in C language which allows us to combine data of different types together.

  • Consider an example If I have to record the particulars of a student then I need to collect his name, age, gender, etc. Then I would have to do this for each student.
  • It is almost similar to an array with the exception that an array encompasses data of similar type.
  • The major difference between an array and structure is that a structure can uphold data of totally different types which makes it more user-friendly to program.
  • Inside a structure, data is collected in the form of records.

Defining a structure:

  • The specific keyword struct is used to define any structure.

STRUCTURE SYNTAX:

Struct[structure_name]

{

[structure_objects];

}

DECLARING STRUCTURE VARIABLES SEPARATELY:

struct student

{

char name[20];

int age;

Char name;

Char gender;

};

DECLARING STRUCTURE OBJECTS:

struct student

{

char name[20];

int age;

Char name;

Char gender;

} St1,St2;

  • Here St1 AND St2 are the objects /variables to the structure student as defined by the user.

Accessing structure members:

  • These elements can be accessed in many amounts of ways.
  • Therefore assignment can be done in multiple ways.
  • For the successful assignment of any value to an object within a structure, the member of the structure has to assigned to a specific operator known as dot operator/period operator.

EXAMPLE:

#include<stdio.h>

#include<string.h>

struct student

{
char name[25];

char branch;

Int age;

char gender;

};

Int main()

{
struct student s1;

s1.age=34;

strcpy(s1.name,”KEVIN”);

printf(“Age of student s1 is %d”,s1.age);

printf(“name of student s1 is %s”,s1.name);

return 0;

}

INITIALIZATION IN STRUCTURE:

  • If you want to directly initialize the structure variables u can do in the following format:-

Void main()
{

Struct Patient

{

float height;

int weight;

Int age

};

Struct patient p1= {6.5, 35,24};

}

ARRAY OF STRUCTURE:

  • Here each individual element of array will represent a structure variable/member.
  • Consider for example STRUCT employee emp[10].
  • This basically denoted that there is a maximum of 10 employee objects for the structure called employee.

NESTED STRUCTURE:

  • There is another condition called nested structure which basically implies a structure contained within a structure.

EXAMPLE:

STRUCTURE 1 STUDENT ADDRESS:

struct stu_address

{

Int street;
Char *state;

Char *state;

Char *country

}

STRUCTURE 2 : STUDENT DATA:

Struct stu_data

{

int stu_id;

Int stu roll no;

Struct stu_address stu_add;

}

Here stu_add is declared as an object common to both structures.

USE OF TYPEDEF IN STRUCTURE:

  • The primary function of this typedef is to make the program a lot legible and easier.
  • It is like an alternative form of structure.

EXAMPLE:

CODE WITHOUT USING TYPEDEF

struct home_add

{

int street;

char *state;

char *country;

char *town;

};

struct home_add var;

var.town = “agra”;

CODE WITH TYPEDEF:

typedef struct home_add

{
int street;

Char *state;

Char *country;

Char *town;

};addr;

addr var;

var.town = “agra”;

STRUCTURE AS FUNCTION ARGUMENTS:

#include<stdio.h>

Struct student

{
Char name[20];

Int roll no;

}

void show(struct student st);

Void main()

{

Struct student std;
printf(“\n Enter the student record name\n:”);

scanf(“%s”,std.name);

printf(“\nEnter the student roll number\n”);

scanf(“%d”,&std.roll no);

show(std);

}
void show(struct student st)

{

printf(\n”Student name is %s\n”,st.name);

printf(“\nStudent roll number is %d”,st.roll no);

}

Leave a comment

We are the best Embedded Training Institutes in Bangalore with 100% job placement record. Our top most priority is to put more and more focus on practical aspects of the Training. We believe if the student is good with practical knowledge he can crack any interview he/she wants. We also promote the meritorious students and those who are in need but can't afford the expensive training in the other institutes are also welcomed at Professional Institute.

It is needless to say that there are many institute which provides Embedded Training in Bangalore but most of them focus on the quantity of the course but we focus majorly on the quality of the education. The reason is very simple, those institute have profit as their main motive but we are mostly concerned with the future of the students. That's why we charge very minimal fees from students.

PTInstitute © 2017 All rights reserved.