0% found this document useful (0 votes)
67 views29 pages

Structures in C Language

The document discusses structures in C programming. It defines a book structure with name, price, and pages elements. It then declares book structure variables and assigns values to them. It also shows how to pass a structure to a function by passing individual elements or the entire structure. Structures allow grouping of related data and functions can access structured data through dot operator or by passing the entire structure.

Uploaded by

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

Structures in C Language

The document discusses structures in C programming. It defines a book structure with name, price, and pages elements. It then declares book structure variables and assigns values to them. It also shows how to pass a structure to a function by passing individual elements or the entire structure. Structures allow grouping of related data and functions can access structured data through dot operator or by passing the entire structure.

Uploaded by

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

struct <structure name> struct book

{ {
structure element 1 ; char name ;
structure element 2 ; float price ;
structure element 3 ; int pages ;
...... };
......
};
struct <structure name> struct <structure name>
{ {
structure element 1 ; structure element 1 ;
structure element 2 ; structure element 2 ;
structure element 3 ; structure element 3 ;
...... ......
...... ......
}; } b1, b2, b3 ;
struct book b1, b2, b3 ;

struct book struct book


{ {
char name ; char name ;
float price ; float price ;
int pages ; int pages ;
}; } b1, b2, b3 ;
struct book b1, b2, b3 ;
struct book
{
char name[ 10 ] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
struct book b2 = { "Physics", 150.80, 800 } ;
struct book b3 = { 0 } ;

Note the following points while declaring a structure type:


(a) The closing brace ( } ) in the structure type declaration must be followed
by a semicolon ( ; ).
(b) It is important to understand that a structure type declaration does not
tell the compiler to reserve any space in memory. All a structure declaration
does is, it defines the ‘form’ of the structure.
(c) Usually structure type declaration appears at the top of the source code
file, before any variables or functions are defined. In very large programs
they are usually put in a separate header file, and the file is included (using
the preprocessor directive #include) in whichever program we want to use
this structure type.
/* copying all elements at one go */
# include <stdio.h> e3 = e2 ;
# include <string.h> printf ( "%s %d %f\n", e1.name,
int main( ) e1.age, e1.salary ) ;
{ printf ( "%s %d %f\n", e2.name,
struct employee e2.age, e2.salary ) ;
{ printf ( "%s %d %f\n", e3.name,
char name[ 10 ] ; e3.age, e3.salary ) ;
int age ; return 0 ;
float salary ; }
};
struct employee e1 = { "Sanjay", 30,
5500.50 } ;
struct employee e2, e3 ;
/* piece-meal copying */
strcpy ( e2.name, e1.name ) ;
/* e2.name = e1. name is wrong */
e2.age = e1.age ;
e2.salary = e1.salary ;
Additional Features of Structures

1) The values of a structure variable can be assigned to another structure


variable of the same type using the assignment operator.
2) This copying of all structure elements at one go has been possible only
because the structure elements are stored in contiguous memory locations.
# include <stdio.h> Address of name = 1699304112
int main( ) size total = 8
{struct book Address of price = 1699304113
{char name; Address of pages = 1699304116
char price ;
int pages ;
};
struct book b1 = { 'B', 130.00, 550 } ;
int c = sizeof(b1);
(b1.name);
printf ( "Address of name = %u\n", &b1.name ) ;
printf ( "size total = %d\n", c ) ;
printf ( "Address of price = %u\n", &b1.price ) ;
printf ( "Address of pages = %u\n", &b1.pages ) ;
return 0 ;
}
# include <stdio.h> Address of name = 3471901240
int main( ) Address of name = 5
{struct book Address of price = 3471901248
{char name[5] ; Address of pages = 3471901252
float price ;
int pages ;
};
struct book b1 = { 'B', 130.00, 550 } ;
int c = sizeof(b1.name);
printf ( "Address of name = %u\n", b1.name ) ;
printf ( “size of name = %d\n", c ) ;
printf ( "Address of price = %u\n", &b1.price ) ;
printf ( "Address of pages = %u\n", &b1.pages ) ;
return 0 ;
}
# include <stdio.h> Address of name = 402412924
int main( ) size of name = 1
{ Address of price = 402412928
struct book Address of pages = 402412932
{char name ;
float price ;
int pages ;
};
struct book b1 = { 'B', 130.00, 550 } ;
int c = sizeof(b1.name);
printf ( "Address of name = %u\n", &b1.name ) ;
printf ( “size of name = %d\n", c ) ;
printf ( "Address of price = %u\n", &b1.price ) ;
printf ( "Address of pages = %u\n", &b1.pages ) ;
return 0 ;
}
#include <stdio.h>
#include <string.h>

// create struct with person1 variable


struct Person {
char name[50];
int citNo;
float salary;
} person1;

int main() {

// assign value to name of person1


strcpy(person1.name, "George Orwell");

// assign values to other person1 variables


person1.citNo = 1984;
person1. salary = 2500;

// print struct variables


printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);

return 0;
}
An array of structures in C can be defined as the collection
of multiple structures variables where each variable contains
information about different entities. The array of
structures in C are used to store information about multiple
entities of different data types. The array of structures is also
known as the collection of structures.
#include<stdio.h> printf("\nStudent Information List:");
#include <string.h> for(i=0;i<5;i++){
struct student{ printf("\nRollno:%d, Name:
int rollno; %s",st[i].rollno,st[i].name);
char name[10]; }
}; return 0;
int main(){ }
int i;
struct student st[5];
printf("Enter Records of 5 students");

for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
OUTPUT
Additional Features of Structures

1) The values of a structure variable can be assigned to another structure


variable of the same type using the assignment operator.
2) This copying of all structure elements at one go has been possible only
because the structure elements are stored in contiguous memory locations.
3) Like an ordinary variable, a structure variable can also be passed to a
function. We may either pass individual structure elements or the entire
structure variable at one shot. Let us examine both the approaches one-by-
one using suitable programs.
/* Passing individual structure elements
*/
# include <stdio.h> OUTPUT:
void display ( char *, char *, int ) ; Let us C YPK 101
int main( )
{
struct book
{
char name[ 25 ] ;
char author[ 25 ] ;
int callno ;
};
struct book b1 = { "Let us C", "YPK", 101 }
;
display ( b1.name, b1.author, b1.callno ) ;
return 0 ;
}
void display ( char *s, char *t, int n )
{
printf ( "%s %s %d\n", s, t, n ) ;
}
# include <stdio.h>
struct book
{ OUTPUT:
char name[ 25 ] ; Let us C YPK 101
char author[ 25 ] ;
int callno ;
};
void display ( struct book ) ;
int main( )
{
struct book b1 = { "Let us C", "YPK", 101 }
;
display ( b1 ) ;
return 0 ;
}
void display ( struct book b )
{
printf ( "%s %s %d\n", b.name, b.author,
b.callno ) ;
}

You might also like