Pointers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Pointers

Module-1
Table of Content
⮚ Introduction

⮚ Basic Concept

⮚ Accessing the Address of a Variable

⮚ Pointer Declarations

⮚ Accessing a Variable Through its Pointer

⮚ Pointer Expressions

⮚ Pointers and Arrays

⮚ Null Pointer
Aim

To equip students in the fundamentals and


understanding of Pointers and its use in Array,
Structure, Function and creation of Linked List.
a. Explain the basics of Pointers to continue the

further implementation of Linked list

b. Applications of Pointers

Objective c. Explain the types of pointers and their use

cases
Introduction

• A pointer is a variable that represents the location


(rather than the value) of a data item.
• They have a number of useful
applications.
– Enables us to access a variable that is defined
outside the function.
– Can be used to pass information back and forth between a
function and its reference point.
– More efficient in handling to access the data.
Basic Concept

• Within the computer memory, every stored data


item occupies one or more contiguous memory
cells.
– The number of memory cells required to store a data
item depends on its type (char, int, double, etc.).
• Whenever we declare a variable, the system
allocates memory location(s) to hold the value of
the variable.
– Since every byte in memory has a unique address, this
location will also have its own (unique) address.
Pointers
#include <stdio.h>
int main()
{
int i = 3, *j, **k; Output:
j = &i; Address of i = 65524
k = &j; Address of i = 65524
printf("Address of i = %u\n", &i);
printf("Address of i = %u\n ", j);
Address of i = 65524
printf("Address of i = %u\n ", *k); Address of j =
printf("Address of j = %u\n ", &j); 65522
printf("Address of j = %u\n ", k); Address of j =
printf("Address of k = %u\n ", &k);
printf("Value of j = %u\n ", j); 65522
printf("Value of k = %u\n ", k); Address of k =
printf("Value of i = %d\n ", i); 65520
printf("Value of i = %d\n ", *(&i));
printf("Value of i = %d\n ", *j);
Value of j = 65524
printf("Value of i = %d\n ", **k); Value of k = 65522
return 0; Value of i = 3
} Value of i = 3
Value of i = 3
Value of i = 3
Contd.

Consider the statement


int xyz = 50;
– This statement instructs the compiler to allocate a location
for the integer variable xyz, and put the value 50 in that
location.
– Suppose that the address location chosen is 1380.
xyz □ variable
50 □ value
1380 □ address
Contd.

• During execution of the program, the system always


associates the name xyz with the address 1380.
– The value 50 can be accessed by using either the name xyz
or the address 1380.
• Since memory addresses are simply numbers, they can
be assigned to some variables which can be stored in
memory.

– Such variables that hold memory addresses are called


pointers.
– Since a pointer is a variable, its value is also stored in
some memory location.
Contd.

• Suppose we assign the address of xyz to a


variable p.
– p is said to point to the variable xyz.

Variable Value
Address p = &xyz;
50
1380

p 1380
1
50
p xyz
Accessing the Address of a Variable

• The address of a variable can be determined using


the ‘&’ operator.
– The operator ‘&’ immediately preceding a variable returns the
address of the variable.
• Example:
p = &xyz;
– The address of xyz (1380) is assigned to p.
• The ‘&’ operator can be used only with a simple variable
or an array element.
&distance
&x[0]
&x[i-2]
Contd.

• Following usages are illegal:


&235
• Pointing at constant.

&(a+b)
• Pointing at expression.
Example
#include <stdio.h>
int main()
{
int a;
float b, c;
double d;
char ch;

a = 10;
b = 2.5;
c = 12.36;
d = 12345.66;
ch = 'A';
printf("%d is stored in location %u \n\n", a, &a);
printf("%f is stored in location %u \n\n", b, &b);
printf("%f is stored in location %u \n\n", c, &c);
printf("%lf is stored in location %u \n\n", d, &d);
printf("%c is stored in location %u\n", ch, &ch);
return 0;
}
OUTPUT:

Incidentally variables a,b,c,d and ch are allocated


to contiguous memory locations.
Pointer Declarations
• Pointer variables must be declared before
we use them.
• General form:
data_type *pointer_name;

Three things are specified in the above


declaration:
1. The asterisk (*) tells that the variable
pointer_name is a pointer variable.
2. pointer_name needs a memory location.
3. pointer_name points to a variable of type data_type.
Contd.

• Example:
int *count;
float *speed;

• Once a pointer variable has been declared, it can be


made to point to a variable using an assignment
statement like:
int *p, xyz;

p = &xyz;

– This is called pointer initialization.


Things to Remember

• Pointer variables must always point to a data item of the same type.
float x;
int *p;

p = x; //will result in erroneous output

• Assigning an absolute address to a pointer variable is prohibited.


int *count;
count = 1268; //absolute address Prohibited
Accessing a Variable Through its Pointer

• Once a pointer has been assigned the address of


a variable, the value of the variable can be
accessed using the indirection operator (*).

int a, b;
int *p;

p = &a;
b = *p; Equivalent to b=a
Example 1
#include <stdio.h>
void main()
{
int a, b;
int c = 5; Equivalent
int *p;
a = 4 * (c + 5);
p = &c;
b = 4 * (*p + 5); Output:
printf("a=%d b=%d \n", a, b);
}
Example 2
#include <stdio.h>
void main()
{
int x, y;
int *ptr;

x = 10;
ptr = &x; // 'ptr' stores the address of 'x'
y = *ptr; // 'y' gets the value stored at the address 'ptr' is pointing to (which is 'x')

printf("%d is stored in location %u \n", x, &x); // Prints value of 'x' and its address
printf("%d is stored in location %u \n", *&x, &x); // Prints value of 'x' and its address (using *&x, which is x)
printf("%d is stored in location %u \n", *ptr, ptr); // Prints value pointed by 'ptr' and the address stored in 'ptr'
printf("%d is stored in location %u \n", y, &y); // Prints value of 'y' and its address
printf("%u is stored in location %u \n", ptr, &ptr); // Prints address stored in 'ptr' and address of 'ptr' itself
printf("%d is stored in location %u \n", y, &y); // Prints value of 'y' and its address again

*ptr = 25; // Modifies the value of 'x' through the pointer 'ptr'
printf("\nNow x = %d \n", x); // Prints the new value of 'x'
}
Output:
Pointer Expressions

• Like other variables, pointer variables


can be used in expressions.
• If p1 and p2 are two pointers, the
following statements are valid:

sum = *p1 + *p2;


prod = *p1 * *p2;
prod = (*p1) * (*p2);
*p1 = *p1 + 2;
x = *p1 / *p2 + 5;
Contd.
• What are allowed in C?
– Add an integer to a pointer.
– Subtract an integer from a pointer.
– Subtract one pointer from
another (related).
• If p1 and p2 are both pointers to the same
array, then p2–p1 gives the number of
elements between p1 and p2.
• What are not allowed?
– Add two pointers.
p1 = p1 + p2 ;
– Multiply / divide a pointer in an
expression.
p1 = p1 – p2 * 10;
p1 = p2 / 5 ;
PASSING POINTER TO FUNCTION
Passing Pointers to a Function
• Pointers are often passed to a
function as arguments.
– Allows data items within the calling
program to be accessed by the function,
altered, and then returned to the
calling program in altered form.
– Called call-by-reference (or by address
or by location).
• Normally, arguments are passed to
a function by value.
– The data items are copied to the
function.
– Changes are not reflected in the
calling program.
Example: passing arguments by VALUE

#include <stdio.h>
void swap(int x, int y);
int main()
{
int a, b;
a = 5;
b = 20;
swap(a, b); a and b do not swap
printf(“\n a = % d, b = % d”, a, b);
return 0;
}
Output
void swap(int x, int y)
{ a = 5, b = 20
int t;
t = x; x and y swap
x = y;
y = t;
}
Example: passing arguments by reference

#include <stdio.h> {
void swap(int *x, int *y); int temp;
int main() temp = *x;
{ *x = *y;
int a, b; *y = temp;
printf("\n Enter the value of a= printf("\n Values of a and b after
"); swaping=%d and %d", *x, *y);
scanf("%d", &a); }
printf("\n Enter the value of b=");
scanf("%d", &b);
printf("\n Values of a and b before
swaping=%d and %d", a, b);
swap(&a, &b); OUTPUT:
return 0;
}
void swap(int *x, int *y)
Pointers and Arrays

• When an array is declared,


– The compiler allocates a base address
and sufficient amount of storage to
contain all the elements of the array 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 constant pointer to the first
element.
Example
• Consider the declaration:
int arr[5] = {1, 2, 3, 4, 5} ;
– Suppose that the base address of x is
2500, and each integer requires 4 bytes.
Element Value Address
arr[0] 1 2500
arr[1] 2 2504
arr[2] 3 2508
arr[3] 4 2512
arr[4] 5 2516
Contd.
arr &arr[0] 2500 ;
– p = arr; and p = &arr [0]; are equivalent.
– We can access successive values of arr by
using p++ or p- - to move from one element to
another.
Note: Relationship between p and arr : *(p+i)
gives the value of arr[i]
p = &arr[0] = 2500
p+1 = &arr[1] = 2504
p+2 = &arr[2] = 2508
p+3 = &arr[3] = 2512
p+4 = &arr [4] = 2516
Example: function to find average
#include <stdio.h> values");
float avg(float array[], int size) for (k = 0; k < n; k++)
{ scanf("%f", &x[k]);
int i;
float *p, sum = 0; printf("\n Average is %f",
p = array; avg(x, n));
for (i = 0; i < size; i++) return 0;
sum = sum + *(p + i); }
return (sum / size);
}
int main()
{
int n, k;
float x[6];
Output:
printf("Enter the number of
items");
scanf("%d", &n);

printf("Enter the array


NULL Pointer
NULL Pointer is a pointer which is pointing to nothing. In case, if we
don’t have address to be assigned to a pointer, then we can simply use
NULL.

#include <stdio.h>
int main()
{
int *ptr = NULL; // Null
Pointer
printf("The value of ptr is
%p", ptr);
return 0;
}
Summary

Outcomes:

a. Understand the basics of Pointers to continue the further

implementation of Linked list

b. Able to identify where to apply Pointers

c. Know the types of pointers and their use cases


Thank you

You might also like