You are on page 1of 24

Pointers & Files

Management
C PROGRAMMING  UNIT 5
Call-by-reference 2

 Formal parameter act as a alias(alternate name) of the


actual parameter.
 Both refer to same memory space that originally allocated
for actual parameter.
 Hence any change made to formal parameter value reflect
back to actual parameter
Call-by-reference Formal 3
param(b).
Actual
int main() param(a)
int add(int b[4])
{
{
int a[ 4] = {5,7,3,4};
int s = 0;
printf(“%d”,a[1]); // prints 7
s = b[0]+ b[1]+ b[2]+b[3];
c = add(a);
b[1] = 10;
printf("SSQ = %d\n", c);
return s;
printf(“%d”,a[1]); // prints 10
}
} 5 7 3 4
The array

After call to ‘add’ array modified as 5 10 3 4


Introduction 4

 A pointer is a derived data type in C. It is derived from other data types which are either
built-in or user defined.

 Whenever a variable is defined in C language, a memory location is assigned for it, in


which it's value will be stored.

 We can easily check this memory address, using the & symbol.

 If var is the name of the variable, then &var will give it's address.
#include<stdio.h>
int main() 5
{
int var = 7;
printf("Value of the variable var is: %d\n", var);
printf("Memory address of the variable var is: %x\n", &var);
/* %x prints address in hexadecimal number system, we can also use %d or %p. %p is
specifically the format specifier for pointers */
return 0;
}
Output: Value of the variable var is: 7
Memory address of the variable var is: bcc7a00
6
 Pointers (also referred as pointer variables) are special variables in C
that hold address of memory locations, rather than some data used in
program.

 Then using these pointers we can directly access the data stored at that
address.

 In short, pointers allow C programs to access the computer memory


directly and manipulate the data store in the memory. This way
accessing data is in fact faster than using a normal data variable.
Benefits of using pointers in C Programs:
1.Pointers are more efficient in handling arrays and data tables.
7
2. They can be used to return multiple values from a function via function arguments.
3. Pointers permit references to functions and thereby facilitating passing of functions as
arguments to other functions.
4. The use of pointer arrays to character strings results in saving of data storage space in memory.
5. Pointers allow C to support dynamic memory management.
6. Pointers provide an efficient tool for manipulating dynamic data structures such as structures,
linked lists, queues, stacks and trees.
7. Pointers reduce length and complexity of programs.
8. They increase the execution speed and thus reduce the program execution time.
Understanding Pointers: 8
 A computer’s memory can be conceptualized as a sequential
collection of storage cells.
 Each cell of the computer’s memory has a number, called an
address, associated with it.
 Typically, the first address of a computer’s memory is
numbered 0.
 On most computer systems, a cell is 1 byte and hence every
byte in memory has a unique address assigned to it.
 Diagram shows memory organization of computer system
with 64 KiloBytes(KB) memory. Recent computers have
memory much more in order of giga bytes.
Underlying concepts of Pointers 9
 Pointers are built on three underlying concepts as illustrated below.
 Pointer constants: Memory address within a computer are referred to as Pointer 10
constants. Normally pointer constants are represented as hexadecimal values(E.g.
0x34AFF34B).

 Pointer Values: As users, we cannot set or assign the pointer address for memory
locations. Rather we can access the memory address value of variable using address
operator(&). This value is called Pointer Value. ( e.g. &quantity).

 Pointer Variables: The Pointer values are stored in some special variables that are
tailored to store the memory addresses. They are known as Pointer Variables or
simple Pointers.
Accessing Address of Variables
11
 Address of a pointer can be accessed using the address operator(&) on the variable name .
 Eg: int quantity = 100;
 &quantity will give the memory address allocated to variable quantity (i.e. place where
data 179 is stored.
 Address operator can be applied only to variable or array name or even function name. It
is illegal to use address operator in the following ways.
 1. &125 → applying & on a constant.
 2. int x[100] , &x (applying & on a array name).
 3. Address operator on at expressions like &(a+b).
 Is x is an array , usage like &x[0] , &x[5] etc are legal. This will give addresses of the
first element and 6th element in the array respectively.
int main()
{
12
char a;
int x; OUTPUT:-
float p, q;  A is stored at addr
3153847627.
a = 'A';
 125 is stored at addr
x = 125;
3153847628.
p = 10.25, q = 18.76;
 10.250000 is stored at addr
printf("%c is stored at addr %u.\n", a, &a); 3153847632.
printf("%d is stored at addr %u.\n", x, &x);  18.760000 is stored at addr
printf("%f is stored at addr %u.\n", p, &p); 3153847636.
printf("%f is stored at addr %u.\n", q, &q);
return 0;
}
Declaring Pointer Variables 13
 Since pointer variables special variables that stores addresses than user data, there is a
slightly different syntax to declare them.
E.g int *p;
 This declaration implies that p is a pointer variable that can store the address of integer type
variable.
 General syntax for declaring a pointer variable is:
data_type *pt_name ;
 This tells three things to compiler about variable pt_name.
1. The Asterisk(*) tells the variable pt_name is a pointer variable.
2. pt_name can point to memory location.
3. pt_name points to variable of type data_type.
 Consider float *x;
 This declares float type pointer variable which can store the address of a floating 14
point variable.

 Pointer declaration Styles: Three styles are common for declaration of pointer
variables:

 int* p; // style 1
 int *p; // style 2
 int * p; // style 3
 Style 2 is recommended and more popular as it gives more clarity to the reader of the
code. E.g.
 int *p,x,*q;
Initialization of Pointer Variables 15

 So far we have declared pointer variables but did not make them point to any valid memory
location. Lets do that now so that the picture is complete.
int quantity = 179;
int *p; // Step 1. Declaring pointer
p = &quantity; // Step 2: Assigning value to pointer.

 Alternatively, declaration and assigning can be done in single step also as:
int *p = &quantity;
16
 Pointer Flexibility:
 Pointer variables are variables in its very essence. Hence they need not bind it self to a 17
single data variable always.
 Means a pointer variable can point different memory locations at different times.
 E.G:
int x,y,z,*p; // p is pointer others integer variables.
. . p = &x;
. . p = &y;
. . p = &z;
 This above usage is just fine. Note that, at any time, p points at only one variable.
 At the same time, it is also possible that multiple pointers can point to same data
variable.
18
 E.g:
int x;
int *p1 = &x;
int *p2 = &x;
int *p3 = &x;
 This can be visualized as :
Accessing Variable Through its Pointer
19
 Accessing the variable means accessing value stored in the variable.
 An operator called Indirection operator(*) is used for that.
int x =100;
int *p = &x; // Creating a pointer to point to x.
printf(“%d”,x) // Outputs 100
printf(“%d”,*p) // Outputs 100

x =200; // Modifying variable x


printf("%d",x) // Outputs 200
printf("%d",*p) // Outputs 200

*p = 300 // Modifying variable x using the pointer p to it.


printf(“%d”,x) // Outputs 300
printf(“%d”,*p) // Outputs 300
#include<stdio.h>  OUTPUT:-
int main(){
int x, y; Value of x is 10 20
int *ptr;
x = 10; 1.10 is stored at addr4104 (Note:
ptr = &x; address value will vary)
y = *ptr;
printf("Value of x is %d\n\n",x); 2. 10 is stored at addr 4104
printf("1. %d is stored at addr %u\n", x, &x);
printf("2. %d is stored at addr %u\n", *&x, &x); 3. 10 is stored at addr 4104
printf("3. %d is stored at addr %u\n", *ptr, ptr);
4. 10 is stored at addr 4104
printf("4. %d is stored at addr %u\n", y, &*ptr);
printf("5. %d is stored at addr %u\n", ptr, &ptr);
5. 4104 is stored at addr 4106
printf("6. %d is stored at addr %u\n", y, &y);
*ptr = 25; 6. 10 is stored at addr 4108
printf("\nNow x = %d\n",x);
return 0; } Now x = 25
21
Pointers and Arrays 22

 There is relation between pointers and arrays in C. When an array is declared (e.g. in
x[5]; ), compiler allocates a base address and sufficient storage to contain all elements.

 Base address is the address of the first element of the array ( i.e. &x[0]).

 In addition, the array name x is defined internally as constant pointer(const int *) to first
element.

E.g. int x[5] = {1,2,3,4,5};


 Assume base address of array is 1000 and each integer is 2 bytes. Below diagram shoes the
memory layout of the array.
23

 Here x =&x[0] = 1000 Consider pointer p,


int * p; p = x; // p = x is equivalent to p = &x[0].
 Now every element of array x can be accessed using p with increments. p+1 = &x[1]
(=1002)
p+2 =&x[2] (=1004)
p+3 =&x[3] (=1006)
p+4 =&x[4] (=1008)
Address of x[3] is computed as 1000 (the base address) + 3 * size of int.
#include<stdio.h> OUTPUT:-
int main() {
Based address of x[5] is 0x7ffc336abfd0 24
int i;
int x[5] = {100,101,103,104,105};
int *ptr = x;
Address of x[0] is 0x7ffc336abfd0 Value
printf("Based address of array x is \t %p\n",x);
of pointer ptr 0x7ffc336abfd0
printf("Address of x[0] is \t %p\n", &x[0]);
printf("Value of pointer ptr \t %p\n",ptr);
for(i = 0;i<5;i++)
0x7ffc336abfd0 contains value 100
{
0x7ffc336abfd4 contains value 101
printf("%p contains value \t %d\n",ptr,*ptr);
ptr++; 0x7ffc336abfd8 contains value 103
} 0x7ffc336abfdc contains value 104
return 0; 0x7ffc336abfe0 contains value 105
}

You might also like