Notes Unit-5 Pointers
Notes Unit-5 Pointers
Pointers
Basic Concepts
In memory, every stored data item occupies one or more
contiguous memory cells (each cell is of 1 byte).
–The number of memory cells required to store a
data item depends on its type (char, int, double, etc.).
xyz Variable
50 Value
1380 address
Contd.
During execution of the program, the system always
associates the name xyz with the address 1380.
…… ……
Address vs. Value
Each memory cell has an address associated with it.
Each cell also stores some value.
……
50 76 ……
Address vs. Value
Each memory cell has an address associated with it.
Each cell also stores some value.
Don’t confuse the address referring to a memory location
with the value stored in that location.
…… 50 76 ……
Values vs. Locations
Variables name and memory locations, hold values.
1024
Value
32
x
Address
Name
Introduction to Pointers
First of all, it is variable, just like other variables you
studied.
– so it has type, storage address, value etc.
pointer_name Variable
(data_type)
(data_type)
Cont..
Declaration of pointer
data_type * variable_name;
int * ptr;
float *speed;
int * xp ;
double *salary;
Cont..
int x;
Pointer to int
int * xp ;
xp 1024
2868
Cont….
Since a pointer is a variable, its value is also stored in
some memory location.
Example- int *p, xyz=50;
p=&xyz;
But remember that p is not an ordinary variable like any
other integer variable. It is a variable that contains the address
of other variable (xyz in this case). Since p is a variable the
compiler must provide it space in the memory. The following
memory map would illustrate the contents of p and xyz.
50 1380
xyz p
1380 2545
Cont….
xyz 50 1380
p 1380 2545
Simple Pointer Example:
#include<stdio.h>
int main()
{
int a = 3;
int *ptr;
ptr = &a;
printf(“%u\n”, &a);
printf(“%u\n”, ptr);
return 0;
}
&235
&(‘b’)
• with constant.
&(a+b)
• with expression.
Example
#include <stdio.h> Address of A: 3221224908
main() Address of B: 3221224904
{ Address of C: 3221224900
int a; Address of D: 3221224892
float b, c; Address of ch: 3221224891
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”, a, &a) ;
printf (“%f is stored in location %u \n”, b, &b) ;
printf (“%f is stored in location %u \n”, c, &c) ;
printf (“%lf is stored in location %u \n”, d, &d) ;
printf (“%c is stored in location %u \n”, ch, &ch) ;
}
Output:
10 is stored in location 3221224908
2.500000 is stored in location 3221224904
12.360000 is stored in location 3221224900
12345.660000 is stored in location 3221224892
A is stored in location 3221224891
ptr = &i;
printf("\nAddress of i : %u",&i);
printf("\nValue of ptr is : %u",ptr);
return 0;
}
Understanding address operator
After declaration memory map will be like this –
int i = 5;
int *ptr;
ptr = &i;
Things to Remember
Pointer variables must always point to a data item of the
same type.
float x;
int *p;
gives warning and will result in erroneous output
:
p = &x;
Assigning an absolute address to a pointer variable is
prohibited. (except NULL or 0 value no other constant value
can be assigned).
int *p = NULL; int*p= 0; // allowed
int *count;
:
count = 1268;// can’t be assigned
How much memory space does a pointer take
#include <stdio.h>
int main()
{
int a = 5;
return 0;
}
Accessing a Variable Through its Pointer
This is done by using unary operator * that returns the
value of the variable located at the address specified by its
operand.
int a, b;
int *p;
: b = a;
p = &a;
b = *p; // assign value of a in
b
Accessing a Variable Through its Pointer
Asterisk Operator is also called as value at address
operator.
Variable Value in it
num 50
&num 1002
ptr 1002
*ptr 50
Accessing a Variable Through its Pointer
int x; Pointer to int
int * p ;
1024: 32
p= &x; X
Address of X
1024
…..
(x == *p) True p
(p == &x) True
p means Address of variable x
*p means
i.e. content of address of variable x
i.e. data of x itself
Accessing a Variable Through its Pointer
int x;
Pointer to int
int * xp ;
xp = &x ; X 0
Address of X 1024
xp 1024
xp – means address of x
*xp- means value at x
*xp = 0; /* Assign 0 to x */
Accessing a Variable Through its Pointer
int x;
Pointer to int
int * xp ;
xp = &x ; X 1
Address of X 1024
xp 1024
return 0;
}
Example
#include <stdio.h>
void main()
{ Equivalent
int a, b;
int c = 5;
int *p;
a = 4 * (c + 5) ;
p = &c;
b = 4 * (*p + 5) ;
printf (“a=%d b=%d \n”, a, b);
}
Output-
a=40 b= 40
void main()
{
int i = 3 ;
int *j ;
j = &i ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *j ) ;
}
Pointer arithmetic
Like other variables, pointer variables can be used in
expressions.
Considering
sizeof(int) as 2
Next Address
Older Address stored in
Data Type stored in pointer after
pointer incrementing
(ptr++)
int 1000 1004
float 1000 1008
char 1000 1001
Decrementing a Pointer
Next Address
stored in pointer
Older Address
Data Type after
stored in pointer
decrementing
(ptr–)
int 1000 0996
float 1000 0994
char 1000 0999
Rules of Pointer Operations
Pointer Variable Can be Assigned the address of another
Variable
int num = 10;
int *ptr;
ptr = #
Pointer Variable Can be Assigned the value of another Pointer
Variable
int *sptr,*tptr, num=2;;
sptr = #
tptr = sptr;
Pointer Variable Can be initialized with zero or NULL value.
int *sptr,*tptr;
sptr = 0;
tptr = NULL;
Rules of Pointer Operations
Pre/Post fix Increment/Decrement Operation Can be Performed
on Pointer variable
int arr[20];
int *ptr;
ptr = &arr;
ptr++;
Integer value can be added or Subtracted from Pointer variable
int arr[20];
int *ptr;
ptr = &arr;
ptr = ptr + 2; //arr[2] will be accessed
Rules of Pointer Operations
A Value cannot be assigned to arbitrary address
int *ptr;
ptr = &100; //Illegal
Pointer Variable cannot be multiplied by Constant
int *ptr;
ptr = ptr * 6; //Illegal
2 Pointer variables cannot be added
int*p, *q;
p= p+q;
Use pointers
• Return one value as usual with a return statement
• For other return values, pass the address of a variable in
which the value is to be returned
■ Thus we have two different ways to write the address of any array
element:
■ We can write the actual array element, preceded by an ampersand; or
we can write an expression in which the subscript is added to the
array name.
Hence, the expression (x + i) is a symbolic representation for an
address specification rather than an arithmetic expression.
x[i] and *(x + i) both represent the contents of that address, i.e., the
value of the ith element of x.
Pointers and Arrays
Consider the declaration:
int x[5] = {1, 2, 3, 4, 5};
Suppose that each integer requires 4 bytes
Compiler allocates a contiguous storage of size 5x4 = 20
bytes
Suppose the starting address of that storage is 2500
p
a b c \0
#include<stdio.h>
int main()
{
char *name=“ABC";
}
Pointers and Strings
#include<stdio.h>
int main()
{
int i;
for(i=0;i<4;i++)
printf(“………. of Character %d : %......\n” ,i,(ptr +i));
return 0;
}
Pointers and Strings
#include<stdio.h>
int main()
{
int i;
for(i=0;i<4;i++)
printf("Address of Character %d : %u\n“ ,i,(ptr +i));
return 0;
}
Differences : string(char array) & char pointer
main( )
{
char str1[ ] = "Hello" ;
char str2[10] ;
char *s = "Good Morning" ;
char *q ;
str2 = str1 ; /* error */
q = s ; /* works */
}
Contd..
Also, once a string has been defined it cannot be initialized
to another set of characters. Unlike strings, such an
operation is perfectly valid with char pointers.
main( )
{
char str1[ ] = "Hello" ;
char *p = "Hello" ;
str1 = "Bye" ; /* error */
p = "Bye" ; /* works */
}
Void Pointer: Introduction
Suppose we have to declare integer pointer,character pointer
and float pointer then we need to declare 3 pointer variables.
Instead of declaring different types of pointer variable it is
feasible to declare single pointer variable which can act as
integer pointer,character pointer and float pointer.
void * pointer_name;