0% found this document useful (0 votes)
10 views

C Program 5

The document discusses structures in C programming, including declaring and defining structures, accessing structure members, nested structures, arrays of structures, and pointers. Structures allow grouping of related data types under a single name. Structure members can be accessed using the dot operator. Pointers are variables that store addresses of other variables.

Uploaded by

Haresh S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

C Program 5

The document discusses structures in C programming, including declaring and defining structures, accessing structure members, nested structures, arrays of structures, and pointers. Structures allow grouping of related data types under a single name. Structure members can be accessed using the dot operator. Pointers are variables that store addresses of other variables.

Uploaded by

Haresh S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

C PROGRAMMING

FOR PROBLEM SOLVING


(18CPS23)
Module 5
Structure and Pointers, Preprocessor Directives

Prof. Mahalakshmi C V, Assistant Professor, CSE, BIT


Email: [email protected]
Website: https://sites.google.com/site/mahalakshmicvbitcse
STRUCTURES

Structures is a collection of elements of different data type, grouped under a


single name.
• Suppose, you want to store the information about person about his name,
citizenship number and salary.
• These information done separately but, better approach will be collection
of these information under single name because all these information are
related to person.
• For this , structure can be used
DECLARATION OF STRUCTURES
• The keyword struct is used to declare structure followed by structure tag.
• The syntax is shown below:

• The variables that are used to store the data are called members of the
structure.
• Structure definition is terminated with a semicolon.
• We can create the structure for a person as shown below:
DECLARATION OF STRUCTURE VARIABLES

• The structure variables can be declared using 2 methods:


– Tagged Structure:
when a structure is defined, it creates a user-defined type but, no
storage is allocated. The syntax for creating structure variable can be
written as:
• Example:
struct student
{
char name[50];
char usn[10];
int age;
float marks;
}s1,s2;
Type Defined Structure

• Another way of creating structure variable is by using the keyword


typedef:

• Here the above statement declares that the variables s1 and s2 are variables
of type STUDENT(which is of type as struct student).
Accessing Members of a Structure

• The members of a structure can be accessed by using(.) dot operator.


• Any member of a structure can be accessed as:

• Structure variable Initialization:


– Initializing a structure means assigning some constant to the members
of structure. The syntax for initializing the structure member variables
is:

Example:
struct student s1={“CANARA”,”4CB”,18,25.0};
• Reading and writing structure variable:
Based on the member type structure members can use input and output
statements for reading writing.
printf(“\n the student details are”);
printf(“\n Name: %s”,s.name);
printf(“\nUSN: %s”,s.usn);
printf(“\n age:%d”,s.age);
printf(“\n marks: %f”, s.marks);
}
STRUCTURES WITHIN STRUCTURES (NESTED STRUCTURES):
A structure within a structure is called nested structure i.e. a structure is a
member of another structure.
We can create nested structures in two ways
1. Placing complete definition of a structure inside the definition of another
structure.
2. Structure is defined separately and a variable of structure can be placed
inside the definition of another structure definition.
• ACCESSING NESTED STRUCTURE MEMBERS:
– SYNTAX:

– EXAMPLE:
S.dob.day=20;
S.dob.month=3;
S.dob.year=2018;
• Array of structures:
An array of structure is declared in the same way as we had declared
array of built in data type.
Syntax:
POINTERS

• A pointer is a variable which holds the address of another variable or a


memory-location of same type.
– The two primary operators used with pointers are *(asterisk) and & (ampersand).
– The * operator is used to define pointer variables and to deference a pointer.
“Dereferencing” a pointer means to use the value of the pointer.
– The & operator gives the address of a variable.
• Declaration of Pointer
Accessing Variables through Pointers

You might also like