Professional Documents
Culture Documents
WINSEM2021-22 CSE1002 LO VL2021220501708 Reference Material I 01-03-2022 Structure Draft1
WINSEM2021-22 CSE1002 LO VL2021220501708 Reference Material I 01-03-2022 Structure Draft1
Unions
Structure
Syntax of a structure
datatype element 1 ;
datatype element 2 ;
datatype element 3 ;
......
};
How to declare structure
variables?
struct Point
{ int x, y;
int x, y; };
} p1; // The variable p1 is declared with 'Point'
int main()
{
struct Point p1; // The
variable p1 is declared like a
// A variable declaration like basic data types normal variable
struct Point }
{
Example for Structures
struct book
char name ;
float price ;
int pages ;
};
Same as
Example for Structures
struct book
char name ;
float price ;
int pages ;
} b1, b2, b3 ;
Same as
Anonymous Structures – Structures without
name
struct
char name ;
float price ;
int pages ;
} b1, b2, b3 ;
Initialization of Structures
struct book {
char name[10] ;
float price ;
int pages ;
};
int main()
{
// A valid initialization. member x gets value 0 and y
// gets value 1. The order of declaration is followed.
struct Point p1 = {0, 1};
}
Accessing Structure Elements
To refer pages,
b1.pages
b1.price
How to access structure
elements?
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
return 0;
}
Values of One Structure Variable to Another
struct employee
{ char name[10] ;
int age ;
float salary ;
};
struct employee e1 = { "Sanjay", 30, 5500.50 } ;
struct employee e2, e3 ;
Values of One Structure Variable to Another
/* piece-meal copying */
e2.age = e1.age ;
e2.salary = e1.salary ;
Values of One Structure Variable to Another
e3 = e2 ;
#include <string.h>
struct employee
{ int id;
char name[50];
int main( )
e1.id=101;
return 0;
}
#include<stdio.h> //store second employee information
#include <string.h> e2.id=102;
strcpy(e2.name, "James Bond");
struct employee e2.salary=126000;
{ int id;
//printing first employee information
char name[50];
printf( "employee 1 id : %d\n", e1.id);
float salary; printf( "employee 1 name : %s\n", e1.name);
}e1,e2; //declaring e1 and e2 variables for structure printf( "employee 1 salary : %f\n", e1.salary);
int main( )
//printing second employee information
{ printf( "employee 2 id : %d\n", e2.id);
//store first employee information
printf( "employee 2 name : %s\n", e2.name);
printf( "employee 2 salary : %f\n", e2.salary);
e1.id=101; return 0;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array }
e1.salary=56000;
Array of Structures
• It is defined as the collection of multiple structures
variables where each variable contains information about
different entities
#include<stdio.h>
// Create an array of structures
struct Point struct Point arr[10];
{ // Access array members
int x, y; arr[0].x = 10;
arr[0].y = 20;
}; printf("%d %d", arr[0].x, arr[0].y);
return 0;
int main() }
{
struct book
Array of Structures
{
char name ;
float price ;
int pages ;
};
struct book b[100] ;
int i;
for ( i = 0 ; i <= 99 ; i++ )
{
printf ( "\n Enter name, price and pages " ) ;
scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
}
Array Of Structure Pointers
Like other primitive data types, we can create an array of.
Filter none
#include <stdio.h>
float points;
};
int main(void) {
// student structure variable
struct student std[3];
// student structure
// student structure pointer
struct student { variable
char id[15]; struct student *ptr = NULL;
return 0;
}
Limitations of C Structures
• Its mostly used with user defined data types, when names of data types get
slightly complicated.
struct student
{
int mark [2];
char name [10];
float average;
}
1st way :
• struct student record; /* for normal variable */
struct student *record; /* for pointer variable */
2nd way :
• typedef struct student status;
• When we use “typedef ” keyword before struct <tag_name> like
above, after that we can simply use type definition “status” in the C
program to declare structure variable.
• Now, structure variable declaration will be, “status record”.
• This is equal to “struct student record”. Type definition for “struct
student” is status. i.e. status = “struct student”
//Structure using typedef: } status;
II. If it is divisible then check if the employee has joined the company
before 01/01/2010
#include <stdio.h>
#include <string.h>
union Data {
int aadhar;
char voterid[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data :
%d\n", sizeof(data));
return 0;
}
Accessing Union Members
• Similar to structures
• Can have array
• Can be nested inside structures
• Structures can be nested within union
C Preprocessor and Macros
• These transformations can be the inclusion of header file, macro expansions etc
• All preprocessing directives begin with a # symbol. For example,
• For ex.
#define PI 3.14
Some of the The #include preprocessor is used to include header files to
C programs
For example,
• #include <stdio.h> common uses of C preprocessor are:
• Here, stdio.h is a header file. The #include preprocessor directive
replaces the above line with the contents of stdio.h header file.
• That's the reason why you need to use #include<stdio.h> before you
can use functions like scanf() and printf()
• You can also create your own header file containing function
declaration and include it in your program using this preprocessor
directive
• #include "my_header.h"
Macros using #define
#include <stdio.h>
#define PI 3.1415
int main()
scanf("%f", &radius);
area = PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}
Function like Macros
• You can also define macros that work in a similar way like a
function call
• This is known as function-like macros. For example,
#define circleArea(r) (3.1415*(r)*(r))
• Every time the program encounters circleArea(argument), it is
replaced by (3.1415*(argument)*(argument)).
• Suppose, we passed 5 as an argument then, it expands as
below:
• circleArea(5) expands to (3.1415*5*5)
Using #define preprocessor