DSA-unit-1
DSA-unit-1
Topics to be covered :
1. Pointers, Structures : Introduction to pointers,
pointers and Arrays, Pointers to pointers, pointers
to functions
2. Introductions to Structures : Declarations,
initializations, Accessing Structures, Internal
implementations of structures
3. Files in C : Text input output with respect to
files in C, Basic file handling functions in C
9
Pointers
Objectives
❏ To understand the concept and use of pointers
❏ To be able to declare, define, and initialize pointers
❏ To write programs that access data through pointers
❏ To use pointers as parameters and return types
❏ To understand pointer compatibility, especially regarding pointers to
pointers
❏ To understand the role of quality in software engineering
1. Introduction to pointers
2. Pointers and Arrays
3. Pointers to pointers
4. Pointers to functions
1. Introduction to Pointers
A pointer is a variable that contains an address of
other variable that holds access data.
Advantages :
•Pointers can be used to return multiple values from a
function via function arguments (output parameters).
•Pointers allow C to support dynamic memory
management.
•Use of pointers increases the execution speed and
thus reduces the program execution time.
Example - Returning MULTIPLE values from a functions
12
Example - Returning MULTIPLE values from a functions
13
Understanding Pointers
0
• Computer’s memory is a
1
sequential collection of storage
2
location.
3
• Each location, commonly known 4
as a byte, is identified by an 5
address associated with it. .
• These addresses are numbered .
consecutively, starting from zero. .
• The last address depends on the .
memory size.
– A computer system having 64K
memory has its last address as
65535. 6553
5
• int quantity = 250;
quantit variabl
y e
250 valu
e
500 addres
0 s
• A variable that can hold address of another
variable is called a pointer variable.
• int quantity = 250;
• int *p = &quantity;
p 5000 501
2
• Since the value of the variable p is the address of the
variable quantity, we may access the value of quantity by
using the variable p, and therefore we say that the
variable p points to the variable quantity. Thus p is the
name of the pointer.
Example on Accessing the Address
of a Variable
• We can access the address of a variable with
the & operator in C.
– We have already used & operator in scanf
function.
• The operator & immediately preceding the
variable returns the address of the variable
associated with it.
– p = &quantity; //assigns the address 5000 (value
decided at run time) to p.
• The & operator can be used only with a simple
variable or an array element.
main()
{
int a;
float p, q;
char choice;
a = 15;
p = 10.25f;
q = 5.46f;
choice = 'Y';
printf("%d is stored at address %u\n", a, &a);
printf("%f is stored at address %u\n", p, &p);
printf("%f is stored at address %u\n", q, &q);
printf("%c is stored at address %u\n", choice, &choice);
}
Output:
15 is stored at address 6356748
10.250000 is stored at address 6356744
5.460000 is stored at address 6356740
Y is stored at address 6356739
Declaring Pointer Variables
• Declaration Syantax : data_type *ptr_name;
– The asterix (*) tells that the variable ptr_name is a pointer variable.
– ptr_name needs a memory location.
– ptr_name points to a variable of type data_type.
• Examples - 1. int *p; // declares the variable p as a pointer variable
that can point to a memory location holding an integer data type.
2. float *q; // declares the variable q as a pointer variable
that can point to a memory location holding float data type.
• Since the memory locations corresponding to p and q have not been
assigned any values, these contain garbage value and therefore point
to unknown locations.
– Example: int *p;
p ? ?
contains points to unknown
garbage location
Initialization of Pointer Variable
• The process of assigning the address of a
variable to a pointer variable is known as
initialization.
– Uninitialized pointers will have unknown
values.
– It is important to initialize pointer variables
before they are used in the program.
• int quantity;
• int *p;
int *p = &quantity;
• p = &quantity;
Initialization of Pointer Variable
• We must ensure that the pointer variables always point to the
corresponding type of data.
– float a, b;
– int sum, *p;
– p = &a; // Wrong
• It is possible to declare combine declaration of simple variable,
declaration and initialization pointer variable in one statement.
– int a, *p = &a;
– Declares a as an integer variable and p as a pointer variable and
then initializes p hold the address of a.
• We can also define a pointer variable with an initial value of
NULL or zero.
– int *p = NULL; // same as int *p = 0;
Accessing a Variable Through its
Pointer
• To access value of a variable using the
pointer, we use the asterisk (*) operator, also
known as the indirection operator or
dereferencing operator.
– int quantity, *p, n;
– quantity = 250;
– p = &quantity;
– n = *p; // equivalent to n = *&quantity; which in
turn // is equivalent to n = quantity;
– Here *p returns the value of the variable quantity.
main()
{
int a, b;
int *p;
a = 15;
p = &a;
b = *p;
printf("Value of a is %d and address is %u\n", a, &a);
printf("Value of a is %d and address is %u\n", *&a, &a);
printf("Value of a is %d and address is %u\n", *p, p);
printf("%d is the address pointed to by %u\n", p, &p);
printf("Value of b is %d and address is %u\n", b, &b);
*p = 25;
printf("Now the value of a is %d", a);
}
Value of a is 15 and address is 6356748
Value of a is 15 and address is 6356748
Value of a is 15 and address is 6356748
6356748 is the address pointed to by 6356740
Value of b is 15 and address is 6356744
Now the value of a is 25
Stag Values stored in locations and their
e a
addresses b p
Declaratio
n
635674 635674 635674
8 a 4 b 0
p
a= 10
10
635674 635674 635674
8 a 4 b 0 p
p= 10 6356748
&a
635674 635674 635674
8 a 4 b 0 p
b= 10 10 6356748
*p
635674 635674 635674
8 a 4 b 0 p
b= 10 10 6356748
*p
635674 635674 635674
• ADD few more Examples
Pointers and Arrays
• When an array is declared, the compiler allocates a base
address and memory to hold the array elements in
contiguous memory locations.
– The base address is the location of the first element (index 0) of
the array.
– The compiler also defines the array name as a pointer to the first
element.
– int a[5] = {4, 10, 8, 5, 20};
– The name a is the base address (pointer) that points to the first
element, a[0] and therefore value of a is 1000.
36
37
Example: function larger
• This generic function will return a larger
value of two values to be compared
• To use a larger function as a generic one,
we will need to write a compare function
for each particular data type. A compare
function will return either a positive or
negative flag value depending on which
value in a compared pair is larger: the first
one or the second one.
38
39
40
41
42
43
44
45
2. Structures :
1. Introduction
2. Declarations, initializations,
2. Accessing Structures,
3.Internal implementations of structures
2. Structures - Introduction
• Arrays can be used to represent a group of data
elements of same type.
• However, we cannot use an array to represent a
collection of data elements of different types using
a single name.
• C supports a constructed (or programmer-defined)
data type called structures, a mechanism for
grouping data of different types.
– A structure can be used to group logically related data
items.
Arrays Vs. Structures
• An array is a collection of related data
elements of same type.
– Structure can have elements of different types.
• An array is a derived data type whereas a
structure is a programmer-defined type.
• An array behaves like a built-in type. We just
declare an array and use it.
– In case of structure, we need to define the format
of the structure before the variables of that type
are declared and used.
Structures help us to organize complex
data in a meaningful way…
• Examples on related data items
– book
• author, title, price, year
– student
• name, roll-no, marks
– address
• name, door_number, street, city, pin
– customer
• name, phone_no, city
– inventory
• item, stock, price
Defining a Structure
• Syntax • Example
struct structure_name struct Book
{ {
data_type member1; char title[20];
data_type member2; char author[15];
… int pages;
data_type member; float price;
}; };
• The keyword struct defines a structure with a name Book to hold
four data fields viz., title, author, pages and price.
– These fields are called structure elements or members.
– Each member may belong to a different data type.
– Book is the name of the structure and is called the structure tag.
– The tag name can be used subsequently to declare variables.
Defining a Structure
• The keyword struct defines a structure to
hold four data fields viz., title, author,
pages and price.
– These fields are called structure elements or
members.
– Each member may belong to a different data
type.
– Book is the name of the structure and is called
the structure tag.
– The tag name can be used subsequently to
declare variables.
Defining a Structure
title array of 20 characters
array of 15
author characters
pages integer
price float