0% found this document useful (0 votes)
24 views13 pages

Pointers

Uploaded by

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

Pointers

Uploaded by

aman jatain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

What happens when we declare a Variable

When we declare a variable, the system allocates space


sufficient to hold the value of the specified type.

For Ex. int number = 178;

This statement requests the system to find some location for


this (integer type ) and store the value there.

number Variable

178 value
4523 Address
178
How to access value of Variable

Ex. int number = 178

We may access the value 178 stored at the location


by using variable name (number), or by using
variable address (4523).

As the addresses are numbers they can be assigned


to some integer variable, which again can be stored
in memory.

 Such variables that hold the address of another variable


are called as Pointer variables.
Introduction to Pointers
Pointers are variables which stores memory address.

Every pointer has a data type associated with it.

A variable declaration with asterisk (*) before the


variable name creates a pointer.

For Example:

int *number creates a pointer number.


Pointers Concepts:

Variable value Address

n 4522 int n;

m int m;
4526

*num 4530 int *num;


523

178

Here num is called as a Pointer.


A pointer variable is denoted by *
Concept of Pointers

Variable value Address


n 4522 int n;
6
m int m;
4526

*num 4530 int *num;


523
n=6;
178
Concept of Pointers

Variable value Address

n 4522 int n;
6

m int m;
4526

*num 523 4530 int *num;

num 4522 n=6;


178
num = &n;

‘&’ operator returns the address of its argument

num = &n sets the pointer num to the address of the variable
n.
Concept of Pointers

Variable value Address

n 4522 int n;
6

m 6 int m;
4526

*num 6 523 4530 int *num;

num 4522 n=6;


178
num = &n;
m = *num
‘*’ operator is called as indirection operator.

Indirection operator sets the value of m to that pointed by


num
Declaring Pointer Variables
Since a pointer variables contains address, it should be declared as
pointer before it is used.
 int *ptr;

Here, the asterisk (*) tells us that the variable is a pointer variable.
 ptr is the name given to the pointer variable .
 ptr points to the memory location of a variable of type int .

Ex: int *ptr; // integer pointer


float *f; // float pointer
Pointer Declaration

This is the process by which compiler allocates


memory for Pointer which initially contains some
unknown value.
int *num
num Name
?

4550 address

Valid declarations:

int* p;
int *p; // most popular way of declaration
int * p;
Initializing Pointers

This is the process of assigning the address of a


variable to a Pointer variable.

Ex 1:
int n;
int *num;
num= &n;

Ex 2:
float f; // wrong
int *p
p= &f;
Example
main()
{
int number, Y;
int *ptr;

value of number is 5
number = 5;
ptr= &number; value of ptr is the address of marks
(=4523)
Y = *ptr
} value of Y is 5
int *j;
int i=10; j=&i;
Variable name
i
j
10 Value in Variable
6734
6734 Address of variable
3256
main()
{ j is a pointer variable that contains address of i.
int i=10;
int *j;
j=&i; address of i=6734
printf(“%d”, &i); address of j=3256
printf(“%d”, &j); value of j=6734
printf(“%d”, j); value of i=10
printf(“%d”, i); value of i=10
printf(“%d”, *j); value of i=10
printf(“%d”, *(&i));
}

You might also like