Structure Notes
Structure Notes
Defining a structure
struct keyword is used to define a structure. struct defines a new data type
which is a collection of primary and derived data types.
Syntax:
struct [structure_tag]
{
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];
As you can see in the syntax above, we start with the struct keyword, then
it's optional to provide your structure a name, we suggest you to give it a
name, then inside the curly braces, we have to mention all the member
variables, which are nothing but normal C language variables of different
types like int, float, array etc.
After the closing curly brace, we can specify one or more structure variables,
again this is optional.
struct Student
char name[25];
int age;
char branch[10];
char gender;
};
Each member can have different datatype, like in this case, name is an array
of char type and age is of int type etc. Student is the name of the structure
and is called as the structure tag.
Declaring Structure Variables
struct Student
char name[25];
int age;
char branch[10];
char gender;
};
struct Student
{
char name[25];
int age;
char branch[10];
char gender;
}S1, S2;
For example:
#include<stdio.h>
#include<string.h>
struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};
int main()
{
struct Student s1;
/*
s1 is a variable of Student type and
age is a member of Student
*/
s1.age = 18;
/*
using string function to add name
*/
strcpy(s1.name, "Viraaj");
/*
displaying the stored values
*/
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
return 0;
}
Age of Student 1: 18
Structure Initialization
struct Patient
{
float height;
int weight;
int age;
};
Array of Structure
We can also declare an array of structure variables. in which each element
of the array will represent a structure variable. Example : struct employee
emp[5];
#include<stdio.h>
struct Employee
{
char ename[10];
int sal;
};
struct Employee emp[5];
int i, j;
void ask()
{
for(i = 0; i < 3; i++)
{
printf("\nEnter %dst Employee record:\n", i+1);
printf("\nEmployee name:\t");
scanf("%s", emp[i].ename);
printf("\nEnter Salary:\t");
scanf("%d", &emp[i].sal);
}
printf("\nDisplaying Employee record:\n");
for(i = 0; i < 3; i++)
{
printf("\nEmployee name is %s", emp[i].ename);
printf("\nSlary is %d", emp[i].sal);
}
}
void main()
{
ask();
}
Nested Structures
Example:
struct Student
{
char[30] name;
int age;
struct Address
{
char[50] locality;
char[50] city;
int pincode;
}addr;
};
Structure as Function Arguments
We can pass a structure as a function argument just like we pass any other
variable or an array as a function argument.
Example:
#include<stdio.h>
struct Student
{
char name[10];
int roll;
};
void show(struct Student st);
void main()
{
struct Student std;
printf("\nEnter Student record:\n");
printf("\nStudent name:\t");
scanf("%s", std.name);
printf("\nEnter Student rollno.:\t");
scanf("%d", &std.roll);
show(std);
}
void show(struct Student st)
{
printf("\nstudent name is %s", st.name);
printf("\nroll is %d", st.roll);
}
typedef in C
typedef is a keyword used in C language to assign alternative names to
existing datatypes. Its mostly used with user defined datatypes, when names
of the datatypes become slightly complicated to use in programs. Following
is the general syntax for using typedef,
ulong i, j;
Application of typedef
typedef can be used to give a name to user defined data type as well. Lets
see its use with structures.
typedef struct
{
type member1;
type member2;
type member3;
} type_name;
Here type_name represents the stucture definition associated with it. Now
this type_name can be used to declare a variable of this stucture type.
type_name t1, t2;
#include<stdio.h>
#include<string.h>
typedef struct employee
{
char name[50];
int salary;
}emp;
void main( )
{
emp e1;
printf("\nEnter Employee record:\n");
printf("\nEmployee name:\t");
scanf("%s", e1.name);
printf("\nEnter Employee salary: \t");
scanf("%d", &e1.salary);
printf("\nstudent name is %s", e1.name);
printf("\nroll is %d", e1.salary);
}
typedef and Pointers
IntPtr x, y, z;