0% found this document useful (0 votes)
5 views27 pages

Module 5

This document provides an introduction to C programming, focusing on structures and pointers. It explains how to define and use structures to group different data types, as well as the concept of pointers, which store memory addresses and allow for dynamic memory management. Key topics include structure declaration, member access, and the advantages of using pointers in programming.

Uploaded by

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

Module 5

This document provides an introduction to C programming, focusing on structures and pointers. It explains how to define and use structures to group different data types, as well as the concept of pointers, which store memory addresses and allow for dynamic memory management. Key topics include structure declaration, member access, and the advantages of using pointers in programming.

Uploaded by

Santosh Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

@azdocuments

INTRODUCTION TO
C PROGRAMMING
SUBJECT CODE: 1BPLC205E/105E

MODULE-5

Name:

USN:

College:

AZ Documents
Your Engineering Study Partner
[Link]
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

INTRODUCTION TO C PROGRAMMING
1BPLC205E/105E
MODULE-05
Structures and Pointers: Introduction, Defining a Structure, Declaring and Accessing
Structure Variables and Members, Structure Initialization, Copying and Comparing
Structure Variables, Array of Structures, Arrays within Structures. Pointers: Introduction,
Understanding Pointers, Accessing the Address of Variable, Declaring pointer variables,
initialization of pointers, accessing variables through its pointer.
Textbook: Chapter 11.1 to 11.6, 11.8, 11.19, Chapter 12.1 to 12.6

STRUCTURES IN C

In C programming, arrays are used to store multiple values of the same data type.

Example:

int marks[10];

Here all elements are integers.

However, sometimes we need to store different types of data together.

Example:
Student information

• Student name → string

• Roll number → integer


• Marks → float

These values have different data types.

Arrays cannot store different data types together.

To solve this problem, C provides a special data type called a structure.

DEFINITION OF STRUCTURE

A structure is a user-defined data type that groups variables of different data types under a single name.

Structures help organize complex data logically.

Example:

[Link] 1
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

Student structure
struct student

char name[20];

int rollno;

float marks;

};

Here the structure contains:


• name

• roll number

• marks

Each member may have a different data type.

Example Applications of Structures

Structures can represent real-world objects such as:

• Student records
• Employee details

• Book information

• Bank accounts

• Product information

Example structure

struct employee

{
char name[30];

int id;

float salary;

};

[Link] 2
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

DEFINING A STRUCTURE
Before using a structure, we must first define its format.

Structure definition only describes the structure template, not the variables.

Example

struct book_bank

char title[50];

char author[50];
int pages;

float price;

};

Explanation

Member Data Type

title string

author string

pages integer

price float

Structure Elements (Members)


The variables inside the structure are called members.

Example members:

title

author

pages

price

These members represent attributes of a book.

[Link] 3
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

Structure Tag
The name given to the structure is called the structure tag.

Example

struct book_bank

Here

book_bank → structure tag

This tag is used to create structure variables later.

General Syntax of Structure Definition

struct structure_name

data_type member1;
data_type member2;

data_type member3;

};

Important rules:
1 Structure definition must end with a semicolon.

2 Each member must be declared separately.

3 Members may have different data types.

Arrays vs Structures

Both arrays and structures are structured data types.

But they differ in several ways.

Arrays Structures

Stores elements of same type Stores elements of different types

Derived data type User-defined data type

Declared directly Must be defined first

Example: marks[10] Example: student structure

[Link] 4
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

Example
Array

int marks[5];

Structure

struct student

char name[20];

int roll;
float marks;

};

DECLARING STRUCTURE VARIABLES


After defining the structure, we must create structure variables.

Syntax

struct structure_name variable_name;

Example
struct book_bank book1;

Here

book1 → structure variable

Declaring Multiple Structure Variables

struct book_bank book1, book2, book3;

Each variable represents one book.


Important Note

Structure members do not occupy memory until structure variables are declared.

Example

struct book_bank

char title[20];

[Link] 5
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

char author[20];
};

Memory is allocated only when we create

struct book_bank book1;

COMBINING STRUCTURE DEFINITION AND DECLARATION

Structure definition and variable declaration can also be combined.

Example
struct book_bank

char title[20];

char author[20];
int pages;

float price;

} book1, book2;

However, this method is not recommended because the structure cannot be reused elsewhere.

TYPEDEF STRUCTURES

C provides a keyword typedef to create a new data type name for a structure.

Example

typedef struct

float real;
float imag;

} complex;

Now we can declare variables as

complex c1, c2;

Instead of writing

struct complex c1;

[Link] 6
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

This simplifies structure declarations.

Example: Complex Number Structure

A complex number has:

• real part

• imaginary part

Example

struct complex
{

float real;

float imag;

};
Structure variables

struct complex c1, c2;

Example values

c1 = 3 + 4i
c2 = 5 + 6i

ACCESSING STRUCTURE MEMBERS

Members of a structure are accessed using the dot operator (.)

Example

[Link]

This means
price of book1

Syntax

structure_variable.member

[Link] 7
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

Example Assignments
strcpy([Link],"BASIC");

strcpy([Link],"Balagurusamy");

[Link] = 250;

[Link] = 120.50;

Explanation

[Link] → title of book1

[Link] → author of book1

Example Using scanf

scanf("%s", [Link]);

scanf("%d", &[Link]);
Here

• [Link] stores the book title

• [Link] stores number of pages

Example Program Using Structures

#include<stdio.h>

#include<string.h>

struct book

char title[20];

char author[20];
int pages;

float price;

};

int main()

struct book b1;

[Link] 8
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

strcpy([Link],"C Programming");
strcpy([Link],"Dennis Ritchie");

[Link] = 300;

[Link] = 500;

printf("Title: %s\n", [Link]);

printf("Author: %s\n", [Link]);

printf("Pages: %d\n", [Link]);

printf("Price: %.2f\n", [Link]);


return 0;

Output

Title: C Programming
Author: Dennis Ritchie

Pages: 300

Price: 500.00

Advantages of Structures

1 Allows grouping of related data


2 Can store different data types
3 Improves data organization
4 Useful for real-world data representation
5 Makes program easier to understand

Structures are user-defined data types that allow grouping variables of different types under a single
name. They are useful for representing complex data such as student records, employee information,
and book details. Structures are defined using the struct keyword and members are accessed using the
dot operator. They provide a powerful way to organize data in C programs.

[Link] 9
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

POINTERS IN C
A pointer is a derived data type in C that stores the memory address of another variable.

In simple terms:

A pointer does not store a normal value like 10 or 25.


Instead, it stores the location (address) of a variable in memory.

Because of this ability, pointers can be used to access and manipulate data stored in memory.

Pointers make C very powerful and flexible compared to many other programming languages.

Why Pointers Are Important

Pointers provide many advantages to programmers.

1. Returning Multiple Values from Functions


Normally a function can return only one value.
Using pointers, a function can modify multiple variables and effectively return multiple values.

2. Passing Functions as Arguments

Pointers allow functions to be passed as arguments to other functions.

Example:
This concept is widely used in callbacks and advanced programming.

3. Saving Memory

Pointers to character strings can reduce memory usage because they avoid storing duplicate data.

4. Dynamic Memory Allocation


Pointers allow programs to allocate memory during runtime using functions such as:

malloc()

calloc()

realloc()

free()

This is called dynamic memory management.

[Link] 10
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

5. Creating Dynamic Data Structures


Pointers help in implementing complex data structures such as:

• Linked Lists

• Stacks

• Queues

• Trees

• Graphs

These structures depend heavily on pointers.

Understanding Computer Memory

Computer memory consists of many storage locations.

Each storage location is called a byte.


Every byte has a unique address.

Example memory representation:

Address Value

1000 20
1001 35

1002 90

1003 50

Addresses are usually numbered sequentially.

For example:

1
2

...

65535

in a 64 KB memory system.

[Link] 11
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

Variables and Memory


Whenever we declare a variable, the computer allocates memory for it.

Example

int quantity = 179;

The system stores 179 somewhere in memory.

Example representation:

Address Variable Value

5000 quantity 179


Here:

quantity → variable name

5000 → memory address

179 → stored value

Accessing Variable Value

We can access the value in two ways:

1. Using the variable name


quantity

2. Using the memory address

5000

What is a Pointer Variable?

A pointer variable is a variable that stores the address of another variable.

Example:
quantity → stored at address 5000

Now suppose we declare:

If p stores 5000, then:

p points to quantity

Representation:

[Link] 12
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

Address Variable Value


5000 quantity 179

5048 p 5000

So

p → holds address of quantity

Therefore:

p is a pointer to quantity

Relationship Between Pointer and Variable

quantity = 179

p = address of quantity

So
p → quantity

We say

p points to quantity

Three Important Concepts of Pointers

Pointers are based on three important concepts.

1. Pointer Constant

Memory addresses are fixed numbers.

Example:

5000
6000

7000

These are pointer constants.

They cannot be changed.

[Link] 13
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

2. Pointer Value
We obtain the address of a variable using the address operator &.

Example:

&quantity

This gives the address of quantity.

This address is called pointer value.

3. Pointer Variable
A variable that stores a pointer value is called a pointer variable.

Example:

int *p;

Here p is a pointer variable.

Address Operator (&)

The address operator & returns the address of a variable.

Example
p = &quantity;

This means:

p stores the address of quantity

If quantity is stored at 5000, then:

p = 5000

Example Program
#include<stdio.h>

int main()

int quantity = 179;

printf("Value of quantity = %d\n", quantity);

[Link] 14
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

printf("Address of quantity = %u\n", &quantity);


return 0;

Output example:

Value of quantity = 179

Address of quantity = 5000

(The address will be different on each run.)

Rules of Address Operator (&)

The & operator can only be used with variables.

Valid Usage

&x
&arr[0]

&arr[i]

Invalid Usage
&125 constant

&(x+y) expression

&x array name

Example

int x[10];

Valid

&x[0]
&x[5]

Invalid

&x

Printing Address Values

Memory addresses are usually printed using

[Link] 15
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

%u
because addresses are unsigned integers.

Example

printf("%u", &x);

Declaring Pointer Variables

Since pointers store addresses, they must be declared before use.

Syntax
data_type *pointer_name;

Example

int *p;

This means
p is a pointer to an integer

Example Pointer Declaration

int quantity = 179;


int *p;

p = &quantity;

Explanation

quantity → stores value

p → stores address of quantity

Pointer Diagram
quantity = 179

Address Variable Value

5000 quantity 179

5048 p 5000

So

p → quantity

[Link] 16
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

DECLARING POINTER VARIABLES


In C, every variable must be declared with its data type.

Since pointer variables store memory addresses, they must also be declared before use.

General Syntax

data_type *pointer_name;

Example

int *p;
Explanation:

• int → data type of the variable being pointed to

• * → indicates pointer

• p → pointer variable name


This means:

p is a pointer to an integer variable

What the Compiler Understands


When the compiler sees

int *p;

it understands three things:

* indicates p is a pointer variable

p needs memory location

p will store address of an integer variable

Example Pointer Declarations

Integer pointer

int *p;

Float pointer

float *x;

Character pointer

[Link] 17
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

char *ch;

Important Note

In

int *p;

int refers to the type of variable being pointed to,


NOT the type of the pointer value itself.

Memory Representation

When a pointer is declared but not initialized, it contains an unknown memory address.

Example
int *p;

Memory may look like:

Address Variable Value

6000 p ???? (garbage address)

This means the pointer points to an unknown memory location.

This is dangerous.

POINTER DECLARATION STYLES

The * symbol can appear in different places.


Style 1

int* p;

Style 2 (Most Common)

int *p;

Style 3
int * p;

Modern programmers mostly use:

int *p;

[Link] 18
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

Multiple Pointer Declarations


Example

int *p, x, *q;

Meaning:

p → pointer

q → pointer

x → normal integer variable

Example Program

int x, *p, y;

x = 10;

p = &x;
y = *p;

Explanation

x = 10

p stores address of x
*p gives value stored at that address

INITIALIZATION OF POINTER VARIABLES

Initialization means assigning an address to a pointer.

If a pointer is not initialized, it may point to random memory.

This can cause program errors or crashes.

Example Initialization

int quantity = 179;

int *p;

p = &quantity;

Explanation

quantity → stores 179

[Link] 19
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

p → stores address of quantity

Declaration + Initialization Together

We can combine them.

int *p = &quantity;

Important:

quantity must be declared before this line.

Important Point

This statement initializes p, not *p.

p = &quantity → correct

*p = &quantity → incorrect

Wrong Pointer Assignment

Example

float x = 10.5;
int *p;

p = &x; // WRONG

Reason:

p expects address of integer

but x is float

This causes incorrect behavior.

Combined Declaration Example

We can write:

int x, *p = &x;

This means

x → integer variable

p → pointer to x

[Link] 20
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

But this is invalid:


int *p = &x, x; // WRONG

because x is declared after p.

NULL POINTER

A pointer can also be initialized to NULL.

Example

int *p = NULL;
or

int *p = 0;

Meaning:

p points to nothing
This is safe initialization.

POINTER FLEXIBILITY

Pointers are flexible.


Same pointer → different variables

Example

int x, y;

int *p;

p = &x;

p = &y;

Now p points to y.

Different pointers → same variable

Example

int x;

int *p, *q;

[Link] 21
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

p = &x;
q = &x;

Both pointers refer to the same variable.

Invalid Pointer Assignment

We cannot assign random addresses.

Example

int *p = 5360; // WRONG


Only valid constants:

NULL

ACCESSING A VARIABLE THROUGH ITS POINTER

Once a pointer stores the address of a variable, we can access the value using the indirection operator
*.

This is also called:

• Dereferencing operator

• Indirection operator

Example
int quantity = 179;

int *p;

p = &quantity;

Now

p → address of quantity

[Link] 22
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

Accessing Value
int n;

n = *p;

Explanation

*p → value stored at address p

Since p points to quantity:

*p = 179

So
n = 179

Equivalent Statements

p = &quantity
n = *p

is same as

n = quantity

Also
n = *(&quantity)

Example Program

#include<stdio.h>

int main()

int x = 10;
int *ptr;

int y;

ptr = &x;

y = *ptr;

printf("Value of x = %d\n", x);

[Link] 23
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

printf("Address of x = %u\n", ptr);


printf("Value using pointer = %d\n", *ptr);

return 0;

Example Memory Diagram

Address Variable Value

5000 x 10
5048 ptr 5000

5052 y 10

Here

ptr → points to x
*ptr → value of x

Changing Variable Using Pointer

Example
*ptr = 25;

Meaning

Put 25 at the address stored in ptr

Since ptr points to x:

x = 25

So this is same as

x = 25;
But it is done indirectly using pointer.

Key Pointer Relationships

Important identities:

[Link] 24
INTRODUCTION TO C PROGRAMMING MODULE-05 AZ Documents

x = *(&x)
&x = &*ptr

x = *ptr

(if ptr points to x)

Pointer variables store memory addresses of other variables. They must be declared using the *
symbol and initialized with the address operator &. Once a pointer stores the address of a variable, the
value can be accessed using the dereferencing operator *. This allows indirect access and modification
of variables in memory.

[Link] 25
Thank You
We’re glad to be part of your engineering journey.
Keep exploring, keep innovating, and keep growing.

AZ Documents
Your Engineering Study Partner
[Link]

Quality notes, simplified explanations, and


student-focused resources for every semester.

Connect With Us:


[Link] [Click Here]

@azdocuments [Click Here]


Join Our Student Community: [Click Here]

© 2025 AZ Documents. All rights reserved.


This material is intended for educational purposes only.
Redistribution without permission is prohibited.

You might also like