1. Gandhinagar institute of technology
subject : CPU(2110003)
Topic Name:
Branch : computer
Batch : CE
Name : Vardhil Patel
Guided By: Prof. __________
Pointer
2. Pointers
A pointer is a variable that holds a memory
address.
This address is the location of another object in
memory.
For example, if one variable contains the address of
another variable, the first variable is said to point to
the second.
Basically, a pointer contains an address of another
variable.
Pointers provide an indirect means of accessing or
retrieving the data from memory.
4. The ‘&’ and ‘*’
Consider the declaration int ab=3;
This declaration tells the compiler
a) Reserve space in memory to hold the
integer value.
b) Associate the name ab with this memory
location.
c) Store the value 3 at this location.
5. The ‘&’ and ‘*’
ab
3
Location name
Value at Location
1000 Address
6. Declaring pointer
Data-type *name;
* is a unary operator, also called as indirection
operator.
Data-type is the type of object which the pointer is
pointing.
Any type of pointer can point to anywhere in the
memory.
* is used to declare a pointer and also to dereference a
pointer.
7. When you write int *,
compiler assumes that any address that it holds
points to an integer type.
m= &count;
it means memory address of count variable is stored
into m.
& is unary operator that returns the memory address.
i.e. & (orally called as ampersand) is returning the
address.
so it means m receives the address of count.
8. Suppose, count uses memory
Address 2000 to store its value 100.
so, m=&count means m has 2000
address.
q= *m
it returns the value at address m.
value at address 2000 is 100.
so, q will return value 100.
i.e. q receives the value at address m.
count=1002000
9. In C every variable must be declared for its data type.
Since pointer variables contain addresses that belong to
a separate data type they must be declared as pointers.
The declaration of pointer variable takes following form:
this tells the compile there things about the variable
pt_name
1. The asterisk(*) tell that the variable pt_name is a pointer
variable
2. pt_name needs a memory location.
3. pt_name points to a variable of type data_type.
For example:-int *q;
here q is pointer variable that can hold the address
data_type *variable_name;
10. The ‘&’ and ‘*’
‘&’ Address of operator
‘*’ Value at address operator. Also called the Indirection
Operator.
‘&a’ Returns the address of variable a.
‘*a’ Returns the value stored in a particular
address.
13. Initializing Pointer
Address of some variable can be assigned to a pointer
variable at the time of declaration of the pointer
variable.
For ex:
int num; int num;
int *ptr=# int *p;
p=#
These two statements above are equivalent to
following statements.
4/25/2017 Ashim Lamichhane 13
14. 1/14/10
NULL
• NULL is a special value which may be assigned to a pointer
• NULL indicates that this pointer does not point to any variable
(there is no pointee)
• Often used when pointers are declared
int *pInt = NULL;
• Often used as the return type of functions that return a pointer to
indicate function failure
int *myPtr;
myPtr = myFunction( );
if (myPtr == NULL){
/* something bad happened */
}
• Dereferencing a pointer whose value is NULL will result in
program termination.
17. 17
Pointer Assignments
Since, the variable j is containing an address, it is
declared as int *j;
This declaration tells the compiler that j will be used to
store the address of an integer value – i.e j points to an
integer.
18. 18
An Example:Pointer Assignments
main()
{
int j=3;
int *k;
k = &j;
printf(“Address of j=%d”,&j);
printf(“Address of j=%d”,k);
printf(“Address of j=%d”,&k);
printf(“Value of k=%d”,k);
printf(“Value of j=%d”,j);
printf(“Value of j=%d”,*(&j));
printf(“Value of j=%d”,*j);
}
Output:
Address of j = 1000
Address of j = 1000
Address of k = 2000
Value of k = 1000
Value of j = 3
Value of j = 3
Value of j = error
19. Pointer Arithmetic There are only two arithmetic operations that can be
used on pointers
Addition
Subtraction
To understand this concept, lets p1 be an integer
pointer with value 2000 address.
int is of 2 bytes
After expression p1++;
P1 contains address 2002 not 2001.
20. Each time p1 is incremented, it will point to next
integer.
The same is true for decrement.
for p1--;
Causes value of p1 to be 1998.
Each time a pointer is incremented, it points to the
memory location of the next element of its base type.
If decremented, then it points to previous element
location.
P1=p1+12; makes p1 points to 12th element of p1 type.
21. Arithmetic Rules
You cannot multiply or divide pointers.
You cannot add or subtract two pointers.
You cannot apply bitwise operators to them.
You cannot add or subtract type float or double to or
from pointers.
22. Pointer Comparison
You can compare two pointers in a relational
expression, example:
if(p<q)
printf(“p points to lower memory than q n”);
Pointer comparison are useful only when two pointers
point to a common object such as an array.
23. 23
Pointer Conversions
One type of pointer can be converted to another type of pointer.
Consider the following example:
main()
{
double x = 100.1,y;
int *p;
/*The next statement causes p to point to double*/
p = (int*)&x;
y = *p;
printf(“The value of x is: %f”,y);
}
24. POINTER TO POINTER (Double Pointer)
C allows the use of pointers that point to other
pointers and these in turn, point to data.
For pointers to do that, we only need to add asterisk
(*) for each level of reference. For example:
int a=20;
int *p;
int **q; pointer to “a pointer to
an integer”
p=&a;
q=&p;
• To refer to variable ‘a’ using pointer
‘q’, dereference it once i.e. *p
• To refer to variable ‘a’ using pointer
‘q’, dereference it twice because
there are two levels of indirection
involved.
• Both *p and **q displays 20 if they
are printed with a printf statement.
25.
26. Double Pointer
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
/* take the address of ptr using
address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %dn", var );
printf("Value available at *ptr =
%dn", *ptr );
printf("Value available at **pptr =
%dn", **pptr);
return 0;
}
OUTPUT
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
27. Array Of Pointers
An array of pointers can be declared as
data_type *pointer_name[size];
For ex:
int *p[10];
This declares an array of 10 pointers, each of which points to an integer.
The first pointer is called p[0], the second is p[1] and so on up to p[9].
Initially, these pointers are uninitialized and they can be used as below.
int a=10, b=100, c=1000;
p[0]=&a;
p[1]=&b;
p[2]=&c; and so on.
28. Relationship between 1-D array and pointer
Array name by itself is an address or pointer.
It points to the address of the first element(0th element
of an array)
If x is 1D array, the address of the first element can be
expressed as &x[0] or as x.
Similarly address of second array element can be
written as &x[1]or x+1.
In general, address on an array element i can be
expressed as &x[i] or x+i
29. • In general address of array element i can be
expressed as &x[i] or x+i
• x[i] and *(x+i) both represents represents the
content of the address.
Array x
Address of
first
array
element
can be
expressed as
&x[0] or
simply x
x[0] x[1] x[2] x[3]
Array x
Address of
second
array
element
can be
expressed as
&x[1] or
simply x+1
x[0] x[1] x[2] x[3]
30. #include <stdio.h>
int main(){
int x[5]={20,40,60,80,100},k;
printf("narray element ttelements value
ttaddressn");
for(k=0;k<5;k++){
printf("x[%d]ttt%dttt%pn",k,*(x+k),x+k );
}
}
array
element
elements
value
address
x[0] 20 0x7fff5bb0
bbb0
x[1] 40 0x7fff5bb0
bbb4
x[2] 60 0x7fff5bb0
bbb8
x[3] 80 0x7fff5bb0
bbbc
x[4] 100 0x7fff5bb0
bbc0
To display array element with their address using array name as a pointer
OUTPUT
• Element k acts as the element number( 0,1,2,3,4)
• x acting array is added with k i.e k is added with the address of first
element, it points to the consecutive memory location.
• Thus &x[k] is same as *(x+k)
31. /* WAP to calculate average marks of 10 students in a subject
using pointer*/
#include <stdio.h>
int main(void){
float marks[10],sum=0;
int i;
float avg;
printf("Enter marks of 10 students: ");
for(i=0;i<10;i++){
scanf("%f",marks+i);
sum+=*(marks+i);
}
avg=sum/10;
printf("nThe average is=%fn", avg);
}
4/25/2017 Ashim Lamichhane 31
mark
s+0
mark
s+1
mark
s+2
mark
s+3
mark
s+4
32. Pointers and 2-D Arrays
A two dimensional array is actually a collection of one
dimensional arrays, each indicating a row (i.e. 2-D array can be
thought as one dimensional array of rows).
It is stored in memory in the row form. For ex.
1 2 3
4 5 6
7 8 9
• Is stored in the row major order in memory as illustrated
below
1 2 3 4 5 6 7 8 9
a[0][0] a[0][1] a[0][2] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2]
65500 65502 65504 65506 65508 65510 65512 65514 65516
a=
33. Syntax for declaration of 2-D array
data_type (*ptr_var)[size2];
Instead of data_type array[size1][size2];
Ex: Suppose x is a two dimensional integer array having 4 rows
and 5 columns. We declare x as
int (*x)[5];
rather than
int x[4][5];
x points to the first 5 element array, which is actually first row of
the two dimensional array.
Similarly x+1 points to the second 5 element array, which is the
second row of the two dimensional array
34. Passing Pointer to a Function
A pointer can be passed to a function as an argument.
Passing a pointer means passing address of a variable
instead of value of the variable.
As address is passed in this case, this mechanism is also
known as call by address or call by reference.
As address of variable is passed in this mechanism, if
value in the passed address is changed within function
definition, the value of actual variable is also changed.
35. #include <stdio.h>
void conversion(char *);
int main(){
char input;
printf("Enter character of your
choice: ");
scanf("%c",&input);
conversion(&input);
printf("The corresponding
character is: %cn",input );
}
void conversion(char *c){
if (*c>=97 && *c<=122)
{
*c=*c-32;
}
else if(*c >=65 && *c<=90){
*c=*c+32;
}
}
Output
Enter Character of Your
Choice: a
The corresponding Char is:
A
Enter Character of Your
Choice: b
The corresponding Char is:
B
36. String And Pointer
As strings are arrays and arrays are closely connected with
pointers, we can say that string and pointers are closely
related.
char name[5]=“shyam”;
As the string variable name is an array of characters, it is a
pointer to the first character of the string and can be used
to access and manipulate the characters of the string.
When a pointer to char is printed in the format of a string, it
will start to print the pointer character and then successive
characters until the end of string is reached.
Thus name prints “shyam”, name+1 prints “hyam”, name+2
prints “yam” and so on.