Session 04
Pointers
1
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.
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 address
Variable in memory
1000
1003
1001 1002 1003 1004 1005 1006 5
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.
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
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
9
Pointer Expressions
• • • •
Expressions involving pointers can be classified as follows: Pointer Assignments Pointer Arithmetic Pointer Comparison Pointer Conversion
10
Pointer Assignments Consider the following example
i
j
5
1000
1000
2000
Here i’s value is 5 and j’s value is i’s address. 11
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.
12
An Example:Pointer Assignments main() {
Output:
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);
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
}
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 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;
15
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 is allowed. The pointers can be compared using relational operators.
16
Non-Permissible Operations on Pointers 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. Address of a variable cannot be altered.
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
18
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); } 19
Pointers v/s Arrays Consider the following program main() { static char arr[10]=“Embedded”; char *s=“Embedded”; Output: printf(“%s\n”,arr); printf(“%s\n”,s); Embedded } Embedded 20
Analysis:Pointers v/s Arrays • In the first declaration “Embedded” gets stored in the array arr[]. • 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.
21
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); } 22
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++ is attempting to change this base address.
23
Arrays of Pointers As there are an array of ints, floats, similarly there can be an array of pointers. An array of pointers represent a collection of addresses.
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
1000
1500
2000
2500
arr[0]
arr[1]
arr[2]
arr[3]
1000
1500
2000
2500
1200
1700
2200
2700
26
Pointers To Pointers • Pointer as we know is a variable which contains an 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.
27
Double Pointers i
j
k
3
1000
2000
1000
2000
3000
28
Double Pointer main() { int i=3; int *j; int **k; j=&i; k=&j; printf(“Address of printf(“Address of printf(“Address of printf(“Value of i printf(“Value of j printf(“Value of k }
i is %d%d%d\n”,&i,j,*k); j is %d%d\n”,&j,*k); k is %d\n”,&k); is %d%d%d%d\n”,i,*(&i),*j,**k); is %d\n”,j); 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 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 must use the full declaration of the main function. In fact, main can actually accept two arguments: one argument is number of command line arguments, and the other argument is a full list of all of the command line arguments.
31
Command Line Parameters The full declaration of main looks like this: Argument COUNT int main ( int argc, char *argv[] )
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. 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 is not available. 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. argv[argc] is a null pointer.
33
An Example:Command Line Parameters Consider the following program #include <stdio.h> int main(int argc, char *argv[]) { int x; printf("%d\n",argc); for (x=0; x<argc; x++) printf("%s\n",argv[x]); return 0; } 34
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 on the 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.
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