C-Programming-Class 4
C-Programming-Class 4
Pointers
1
Session Objectives
2
Session Topics
• Concept of pointers
• Address of a variables
• Initialization of pointer
• Pointer arithmetic
• Pointers to strings
• Pointers to pointers
• Pointer to functions
3
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
Pointers
Memory Variable in
address memory
1000 1003
1001
1002
1003
1004
1005
1006
5
The ‘&’ and ‘*’
6
The ‘&’ and ‘*’
ab Location name
3 Value at Location
1000 Address
7
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.
8
main() An Example:Pointers
{
int j=3;
printf(“Address of j=%d\n”,&j);
printf(“Value of j=%d\n”,j);
printf(“Value of j=%d\n”,*(&j));
} Output:
Address of j = 1000
Value of j = 5
Value of j = 5
9
Pointer Expressions
10
Pointer Assignments
Consider the following example
i j
5 1000
1000 2000
11
Pointer Assignments
12
An Example:Pointer Assignments
main() Output:
{ Address of j = 1000
int j=3;
Address of j = 1000
int *k;
k = &j; Address of k = 2000
printf(“Address of j=%d”,&j); Value of k = 1000
printf(“Address of j=%d”,k); Value of j = 3
printf(“Address of j=%d”,&k);
Value of j = 3
printf(“Value of k=%d”,k);
Value of j = 3
printf(“Value of j=%d”,j);
printf(“Value of j=%d”,*(&j));
printf(“Value of j=%d”,*j);
}
13
Operations on Pointers
Pointer variables can be used in various types of
expressions.
Consider i,j,a,p1 and p2 are pointer
variables.
• The following operations are valid:
y = *i * *j;
count = p1 – p2;
p1--;
a = p1 + 2;
b = p1 – 3;
14
Operations on Pointers
15
Permissible Operations on Pointers
16
Non-Permissible Operations on Pointers
17
Pointer Comparisons
Comparison of two pointers can be done.
Generally,pointer comparisons are useful only when two
pointers point to a common object.
Example:If p and q are pointers then the following
statement is valid.
if(p<q)
printf(“p points to lower memory
than q”);
18
Pointer Conversions
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);
}
19
Pointers v/s Arrays
20
Analysis:Pointers v/s Arrays
21
Pointers v/s Arrays
22
Analysis: Pointers v/s Arrays
23
Arrays of Pointers
24
An Example:Arrays of Pointers
main()
{
int *arr[4];
int i=5,j=10,k=15,l=20,m;
arr[0]=&i;
arr[1]=&j;
arr[2]=&k;
arr[3]=&l;
for(m=0;m<=3;m++)
printf(“%d”,*(arr[m]));
}
25
Arrays of Pointers
i j k l
5 10 15 20
26
Pointers To Pointers
27
Double Pointers
i j k
3 1000 2000
28
Double Pointer
main()
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(“Address of i is %d%d%d\n”,&i,j,*k);
printf(“Address of j is %d%d\n”,&j,*k);
printf(“Address of k is %d\n”,&k);
printf(“Value of i is %d%d%d%d\n”,i,*(&i),*j,**k);
printf(“Value of j is %d\n”,j);
printf(“Value of k is %d\n”,k);
}
29
Double Pointer
OUTPUT:
Address of i is 1000 1000 1000
Address of j is 2000 2000
Address of k is 3000
Value of i is 3 3 3 3
Value of j is 1000
Value of k is 2000
30
Command Line Parameters
31
Command Line Parameters
The full declaration of main looks like this:
Argument
COUNT
Argument
VECTOR
32
Command Line Parameters
The integer, argc is the argument count. It is the number of arguments
passed into the program from the command line, including the name of
the program.
After that, every element number less than argc is a command line
argument. You can use each argv element just like a string, or use argv
as a two dimensional array.
33
An Example:Command Line Parameters
35
Where are Command Line Parameters used?
Shell Scripting
Internet Solutions
File System Functions
36
Summary
• A pointer is a variable that holds a memory address.
• Basically, a pointer contains an address of another variable.
• Pointers provide an indirect means of accessing or retrieving the
data from memory.
• Arithmetic operations such as addition, multiplication and
division on two pointers are not allowed.
• A pointer variable cannot be multiplied/divided by a constant or
a variable.
• A pointer which contains an address of another pointer variable
can be defined as a pointer to a pointer or DOUBLE POINTER.
37
Thank You!
38