c-programming-class 4
TRANSCRIPT
-
8/14/2019 C-Programming-Class 4
1/38
1
Session 04
Pointers
-
8/14/2019 C-Programming-Class 4
2/38
2
Session Objectives
Pointer and pointer definition. Use of pointers in accessing single and
double dimensional arrays. String handling using pointers. Concept of singly and doubly linked lists.
-
8/14/2019 C-Programming-Class 4
3/38
3
Session Topics
Concept of pointers Address of a variables Initialization of pointer
Pointer arithmetic Pointers to strings Pointers to pointers
Pointer to functions
-
8/14/2019 C-Programming-Class 4
4/38
4
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 thesecond.Basically, a pointer contains an address of another variable.Pointers provide an indirect means of accessing or retrieving the data from memory.
-
8/14/2019 C-Programming-Class 4
5/38
5
Pointers
Variable inmemory
1000
1001
1002
1003
1004
1005
1006
1003
Memoryaddress
-
8/14/2019 C-Programming-Class 4
6/38
6
The & and *
Consider the declaration int ab=3;This declaration tells the compiler
Reserve space in memory to hold the integer value. Associate the name ab with this memory location. Store the value 3 at this location.
-
8/14/2019 C-Programming-Class 4
7/38
7
The & and *
3
ab
1000
Location name
Value at Location
Address
-
8/14/2019 C-Programming-Class 4
8/38
8
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/14/2019 C-Programming-Class 4
9/38
9
An Example:Pointers main(){
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
-
8/14/2019 C-Programming-Class 4
10/38
10
Pointer Expressions
Expressions involving pointers can beclassified as follows:
Pointer Assignments Pointer Arithmetic Pointer Comparison Pointer Conversion
-
8/14/2019 C-Programming-Class 4
11/38
11
Pointer Assignments
Consider the following example
i
5
1000
1000
2000
j
Here is value is 5 and js value is is address.
-
8/14/2019 C-Programming-Class 4
12/38
12
Pointer Assignments
Since, the variable j is containing anaddress, 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.
-
8/14/2019 C-Programming-Class 4
13/38
13
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 = 3
-
8/14/2019 C-Programming-Class 4
14/38
14
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;
-
8/14/2019 C-Programming-Class 4
15/38
15
Operations on Pointers
Consider i,j,a,p1 and p2 are pointer variables. The following operations are invalid:
p1/2;
p1*2;p1*p2;
p1 + p2;
p1/p2;
-
8/14/2019 C-Programming-Class 4
16/38
16
Permissible Operations on Pointers
A pointer can be decremented or incremented.Addition of two integers to pointers is allowed.
Subtraction of two pointers of same data type isallowed.The pointers can be compared using relationaloperators.
-
8/14/2019 C-Programming-Class 4
17/38
17
Non-Permissible Operations on Pointers
Arithmetic operations such as addition,multiplicationand division on two pointers are not allowed.A pointer variable cannot be multiplied/divided by aconstant or a variable.Address of a variable cannot be altered.
-
8/14/2019 C-Programming-Class 4
18/38
18
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 followingstatement is valid.if(p
-
8/14/2019 C-Programming-Class 4
19/38
19
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 todouble*/
p = (int*)&x;y = *p;
printf(The value of x is: %f,y);}
-
8/14/2019 C-Programming-Class 4
20/38
-
8/14/2019 C-Programming-Class 4
21/38
21
Analysis:Pointers v/s Arrays
In the first declaration Embedded gets stored in the arrayarr[].
In the second declaration Embedded gets stored at some place in the memory and the base address is stored in s.
s points to the zeroth character in the string. Mentioning the name of the array arr, we get its base
address.We can say that an array acts as a pointer to a character.
-
8/14/2019 C-Programming-Class 4
22/38
22
Pointers v/s Arrays
Consider the following program main()
{static char arr[10]=Embedded;char *s=Embedded;s++;arr++;
printf(%s\n,arr); printf(%s\n,s);
}
-
8/14/2019 C-Programming-Class 4
23/38
23
Analysis: Pointers v/s Arrays
s++ increments s such that it starts pointing to m of Embedded and hence would print mbedded as
output. arr++ would give an error message because the
information known to us is the base address and arr++ isattempting to change this base address.
-
8/14/2019 C-Programming-Class 4
24/38
24
Arrays of Pointers
As there are an array of ints, floats, similarly there can bean array of pointers.An array of pointers represent a collection of addresses.
-
8/14/2019 C-Programming-Class 4
25/38
25
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
-
8/14/2019 C-Programming-Class 4
26/38
26
Arrays of Pointers
1000 250020001500
i
5 10
j
2015
lk
2500200015001000arr[0] arr[3]arr[2]arr[1]
1200 270022001700
-
8/14/2019 C-Programming-Class 4
27/38
27
Pointers To Pointers
Pointer as we know is a variable which containsan address another of variable.
A pointer which contains an address of another pointer variable can be defined as a pointer to a pointer or DOUBLE POINTER .
-
8/14/2019 C-Programming-Class 4
28/38
28
Double Pointers
3
i
1000
1000
2000
j
2000
3000
k
-
8/14/2019 C-Programming-Class 4
29/38
-
8/14/2019 C-Programming-Class 4
30/38
-
8/14/2019 C-Programming-Class 4
31/38
31
Command Line Parameters
Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating
system.To use command line arguments in the program, you mustuse the full declaration of the main function.In fact, main can actually accept two arguments: oneargument is number of command line arguments, and theother argument is a full list of all of the command linearguments.
-
8/14/2019 C-Programming-Class 4
32/38
-
8/14/2019 C-Programming-Class 4
33/38
33
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.
The array of character pointers is the listing of all the arguments.argv[0] is the name of the program, or an empty string if the name isnot available.
After that, every element number less than argc is a command lineargument. You can use each argv element just like a string, or use argvas a two dimensional array.
argv[argc] is a null pointer.
-
8/14/2019 C-Programming-Class 4
34/38
34
An Example:Command Line Parameters
Consider the following program#include int main(int argc, char *argv[]){
int x;printf("%d\n",argc);
for (x=0; x
-
8/14/2019 C-Programming-Class 4
35/38
35
Analysis
The char *argv[] line is an array of pointers to string. In other words, each element of the array is a pointer, and each pointer
points to a string (technically, to the first character of the string).
Thus, argv[0] points to a string that contains the first parameter onthe command line (the program's name), argv[1] points to the next
parameter, and so on. The argc variable tells you how many of the pointers in the array are
valid. You will find that the code does nothing more than print each of
the valid strings pointed to by argv .
-
8/14/2019 C-Programming-Class 4
36/38
36
Where are Command Line Parameters used?
Shell ScriptingInternet SolutionsFile System Functions
-
8/14/2019 C-Programming-Class 4
37/38
37
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 .
-
8/14/2019 C-Programming-Class 4
38/38
38
Thank You!