0% found this document useful (0 votes)
55 views5 pages

Structues in C

Structures in C allow programmers to combine different data types together. A structure defines a new data type consisting of a number of named members of different types. This makes it possible to group together student details like name, roll number, marks etc. which have different data types. Structures are defined using a struct tag and members are accessed using the dot operator. Arrays of structures can be used to store information of multiple students. Structures can also be passed to functions by value or by reference. Structure pointers allow accessing structure variables through a pointer using the arrow operator.

Uploaded by

sachinswargam26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views5 pages

Structues in C

Structures in C allow programmers to combine different data types together. A structure defines a new data type consisting of a number of named members of different types. This makes it possible to group together student details like name, roll number, marks etc. which have different data types. Structures are defined using a struct tag and members are accessed using the dot operator. Arrays of structures can be used to store information of multiple students. Structures can also be passed to functions by value or by reference. Structure pointers allow accessing structure variables through a pointer using the arrow operator.

Uploaded by

sachinswargam26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

STRUCTUES IN C

Collection of dissimilar dataelements of

same or different datatypes


A Variable can store only - one type of data Simliarly,
An Array can store one or more data but of similar data type, But in a program if
we want to deal with different data types together we need Structures
For Ex: A student Details with lot of different attributes having different data types
like Name, Address, Roll No, Marks etc All these datatypes if we combined the code
gets easier. So using Structures we can combined all the different datatypes
together.
SYNTAX struct tag_name{
Member 1; (char name{15});
Member 2; int rollno;
Member 3; int age;
}
#If we assign structure variable is 0 to any structure then all the elements in that
structure will be default to 0.
For ex struct student Nikhil={0};
#If we want to copy the values of one structure to another structure then for ex
Struct student Nikhil-{
Nikhil;
6543;
12;
}
Structure student Shashank;
Shashank = Nikhil;
# If we want to copy the element by element from one structure to another
structure variable then for ex
Struct student Nikhil-{
Nikhil;
6543;
12;

}
Structure student Shashank;
Strcpy{Shashank.name,Nikhil.name}

NESTED STRUCTURES

creating a structure variable

inside the structure


For ex:
Structure address {
Char street[15];
Int Hno;
};
Structure student{
Char name[25];
Int rollno;
Struct address addr;
};
Int main(){

or int main(){

Struct student anil;

anil,

Strcpy(anil.name,anil);

1234,

Anil.rollno = 1234;

mg road,

Strcpy(anil.addr.street,mg road);

65,

Anil.addr.Hno= 68;

};

Pf()
}

ARRAY OF STRUCTURES

Storing many students


information like 100s or more creating each student and string there information is
very difficult so we use array of structures.

Struct student{
Char name[15];
Int rollno;
};
Int main(){
Int size=3;
Struct student s[size];
Int counter;
For(counter=0;conunter<size,counter++){
Printf(Enter the name and roll no of student %d\n,counter+1);
Scanf(%s%d,&s[counter].name,&s[counter].rollno);
}
Printf(\n\n);
For(counter=0;counter<size;counter++){
Printf(name %s\t rollno %d\n,s[counter].name,s[counter].rollno);
}

PASSING STRUCTURE TO A FUNCTION


struct student{
char name[15];
int rollno;
};
Void display(char[],int); //taking elements of structures individually
Void show(struct student); // taking entire structure variable as parameter (make
sure to define function before prototyping
Int main(){
Struct student anil;
Strcpy(anil.name,anil);
Anil.rollno=1234;

Display(anil.name,anil.rolln0);
Show(anil);
Getch();
Return(0);
}
Void display(char n[],int r){
Pf(in display function\n);
Pf(%s\t%d\n,n,r);
}
Void show(struct student s){
Pf(in show function\n);
Pf(%s\t%d\n,s.name,s.rollno);
}

STRUCTURE POINTERS IN C
Struct student{
Char name[15];
Int roll no;
};
Int main(){
Struct student anil;
Struct student *ptr;
Strcpy(anil.name,anil);
Anil.rollno=1234;
Ptr=&anil; // assigning anil to a pointer
Pf(%s\t%d\n,anil.name,anil.rollno);
Pf(%s\t,%d,ptr->name,ptr->rollno); \\ getting sturucture var through
pointer using arrow

PASSING STRUCTURE TO A FUNCTION BY


REFERENCE
First we define structure student then
We prototype the display function which gonnna take address of structure and in
the main function we call the display function and pass the address to the display
function.

You might also like