Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 39

POINTERS

due to which ‘C is a unique


language
Memory organization
When program is to run … The executable code is
loaded in primary/main memory… & The variable
gets memory Address
0
Operating system 1
Memory cell code
2
3
4
5
Program code 6
7
8
Heap area

65533
Auto variables on stack 65534
65535
Representation of a Variable
There are three different things associated with a
variable…..
-name of the variable
-Value of the variable
-Address of the variable
count Name
address 155 value
4000
The number of bytes required for a variable
depends on data type of variable and processor
word size.
Accessing address of a variable
Aim : want to know address of x variable…
Answer:
Apply “address of” operator ‘&’ to the
variable name.
i.e.
int x=100;
printf(“address of x %u”, &x);
printf(“value of x %d”,x);
LValue and RValue
Every variable name is associated Lvalue and
Rvalue.
When you do X = 5; then ….
5 goes to
X

So, whenever variable name is on left side we


should have a box to store value on the RHS
within that box.
So, here its Lvalue of X ….. That’s actually
address of x.
Why ‘&’ used in scanf?
scanf(“%d”,&x);
What ever is read is suppose to store in
box named x….. That is Lvalue/address
of x…………….

If we write only x then it means value of


x and not to store at x.
Is it valid? “ &x = 10;”
Here in
&x = 10;
We try to give address of variable x to 10.
But address of any variable is fix from the
point it gets memory. The memory is
allocated by the system…
We can’t change at later stage the
address/position of the variable.
So, &x= 10; is INVALID.
Pointer as a variable
The variable which stores address of another
variable is called pointer to that variable.
variable value Address
quantity 179 5000

ptr 5000 5059

Here ptr is a variable which stores address of


other variable named quantity.
Declaring pointers
Pointer variable contains address but the
address interpretation depends on the type of
data pointed by.
So, Declaration of pointer variable….
data_type *pt_name;
Which tells the compiler that pt_name is a
pointer variable which stores starting address
of an data_typed variable………pt_name points
to a variable of type data_type.
Initializing a pointer
A pointer variable can be made to point
to a variable using an assignment
statement …
ptr = &quantity; Ptr points to quantity variable
Quantity
2500
10
3000 2500
ptr
Before a pointer is initialized, it should not be
used. Pointer initialization …
Float *ptr = y; where y is float variable.
 Following should be avoided….
float x=10; int *y;
y = &x; // invalid because cannot
convert float* to int*
int *ptr;
ptr = 4545; // Invalid
 Be Careful….
‘&’ operator can be used only with a

simple variable or an array element.


int i , j , a[10];
int *ptr;
ptr = &120; // invalid
ptr = &a; // invalid
Accessing a variable using pointer
Indirection operator ‘*’ also named as ‘value at ‘
address……
int quantity=100,*p,n;
P=&quantity; // p points to quantity
n = *p; // assign value at p to n…
Same as n = *&quantity;
You can not access the value stored at the
address 5368 by writing *5368. It will not work.
*p = 70; ?
Pointer Expressions
Y = *p1 * *p2; same as
y = (*p1) * (*p2)
… sum = sum + *p1
Z = 5 * - *p2 / *p1;
same as ( 5 * (- (*p2)) ) / ( *p1 )
*p2 = *p2 + 10;
Que…??
Int x,y=10; x = &y; *x …..
Pointer arithmetic
Add/Subtract integers to/from pointers.
It also allows to subtract one pointer from
another. P1 + 4 … p2 -2 …
P2 – P1, p2-p1 gives no. of elements
between p2 and p1.
Addition of two pointers not allowed. i.e
p1+p2 not allowed.
P1++,++p2,--p1, sum+= *p2; allowed
P1>p2, P1==p2,….. allowed.
Pointer increment and scale factors.
P1++; will cause the pointer p1 to point
to the next value of its type.
i.e if p1 is 2000 .. P1 points to integer…
Than p1++ will make p1 to 2002… the
address is incremented by sizeof int.
???? float var1,*ptr=&var1;
ptr++; ptr++;
printf(“%d”,ptr-&var1);
Pointers and arrays
When array is declared, the compiler
allocates a base address and sufficient
amount of storage contain all the
elements of the array in contiguous
memory location.
Example…
For example,
int x[5]={1,2,3,4,5};
int *p;
Then we can make pointer p to point to the array
x by the following assignment:
p=x;
This is equivalent to
p=&x[0];
Now, we can access every value of x using p++
to move from one element to another.
Continue….
Means
P = &x[0] (= 1000)
P+1 = &x[1] (= 1002)
P+2 = &x[2] (= 1004)
P+3 = &x[3] (= 1006)
P+4 = &x[4] (= 1008)
Program:
1)Aim:- W.A.P. to find sum of two values

using pointer.
2)Aim:- W.A.P. using pointers to find the
biggest of two given numbers.
main()
{
int i,j=25;
int *pi,*pj=&j;
*pj=j+5;
i=*pj+5;
pi=pj;
*pi=i+j;
}
Find the values of:
1) &i 9) i
2) &j 10) pi
3) pj
4) *pj
5) *pi
6) (*pi+2)
7) *(pi-1)
8) (pi+2)
Answers:
1) &i :65490 (address of i)
2) &j :65492 (address of j)
3) pj :65492 (value stored in
pj,address of j)
4) *pj: 65 (value referred by pj)
5) *pi: 65 (value referred by pi)
6) (*pi+2):67 (65+2)
7) *(pi-1):35 (65492 – 1*sizeof(int))
Answers:
8) (pi+2): 65496
(65492+2*sizeof(int))

9) i: 35

10)pi: 65492(value stored in pi)


Pointers and character strings
We can use pointer to declare a string variable
For example:- char *name;
Declares name as string variable.
In C, a constant character string always
represents a pointer to that string.
Therefore, following statements are valid:
char *name; name = “ABC”;
This type of assignment does not apply to
character array.
char name[10];name=“ABC”; INVALID
Pointers and character strings.
We can use pointers to access the individual
characters in a string.
char name[20];
char *cptr = name;
Declares cptr as a pointer to a character and
assigns the address of the first character of name
as the initial value.
Consider the following declaration:
char name[3][15] = {“New Zealand”,
”Australia”,“India”};
Then the memory allocation for this is :

Ne w Z e a l a n d \0

A u s t r a l i a \0
I n d i a \0
Then the storage requirements for the name
table are 45 bytes.
But if we declare this using pointer,
char *name[3] = {“New Zealand”,
“Australia”,“India”};
declares name to be an array of three pointers
to characters, each pointing to a string of
varying length.
This declaration allocates only 28 bytes that are
sufficient to hold all characters.

Ne w Z e a l a n d \0

A u s t r a l i a \0
I n d i a \0
So,
name[0]New Zealand
name[1]Australia
name[2]India

The following statements would print out all the three names:-
for(1=0;i<=2;i++)
printf(“%s\n”,name[i]);

To access j th character in the i th name,we may write as


*(name[i]+j)

NOTE:-The difference between the notations *p[3] -> p as an


array of 3 pointers and (*p)[3]-> p as a pointer to an array of 3
elements.
Pointers and Functions
When we pass addresses to a function, the
parameters receiving the addresses should be
pointers.
The process of calling function using pointers to
pass the addresses of variable is known as call by
reference.
The function which is called by ‘reference’ can
change the value of the variable used in the call.
Example of pointer as a fun.argument
main(){
int x=20;
change(&x);
printf(“%d\n”,x);
}
change(int *p)
{ *p = *p+10; }

Here, when the function change() is called, the address


of the variable x, not the value, is passes into the
function change(). So whatever there is change inside the
function are reflected also in the calling function.
Example of pointer as a fun.argument
Pointer parameters are commonly employed in string
functions.
Example:-
Copy(char *s1,char *s2)
{
while(*s1++ = *s2++) !=‘\0’)
}
The function copy is copying one string to another.
Thus s1,s2 are the pointers to character string.
Functions Returning Pointers
We have seen that function can return a single value by
its name or return multiple values through pointer
parameters.
Since pointers are data type in C,we can also force a
function to return a pointer to the calling function.
Ex:-main(){ int *p;
p=largest(&a,&b); //fun. call
}
int* largest(int *x,int *y) //fun. definition
{
if(*x>*y)
Pointers to Functions
A function, like a variable, has an address
location in the memory.So it is also possible to
declare a pointer to a function.
For example, type (*fptr)();
This tells the compiler that fptr is a pointer to a
function which return type value. Here, the
parenthesis around *fptr are necessary.
type *gptr();
would declares gptr as a function returning a
pointer to a type.
Example…..
int (*p1)(), add();
p1=add;
Declares p1 as pointer to a function and add()
as a function and then make p1 to point to the
function add().
Then,
(*p1)(x,y)
is equivalent to add(x,y).
Pointers and structures
The name of the array is the address of its
zeroth element.
If the product is an array variable of struct type,
then product represents the address of zeroth
element.
Example….
struct inventory{
char name[20];
int number;
float price;
} product[2],*ptr;
This declares product as an array of two elements
of type struct inventory and ptr as a pointer to
data objects of the type struct inventory.
Then the assignment ptr = product;
would assign the address of zeroth
element of product to ptr.
So now ptr will point to product[0].
Its member can be accessed using arrow
operator(->).
ptr->name;
ptr->number;
ptr->price;
Means here ptr-> is other way of writing
product[0].
Then we can made it to point to product[1] by
incrementing it. (ptr++)
Also (*ptr).number is equivalent to
ptr->number.

The parentheses around *ptr are necessary


because the member operator “.” has a higher
precedence than the operator *

You might also like