Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area

Similar presentations


Presentation on theme: "Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area"— Presentation transcript:

1 Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Hrs 8 Pointers 3 Pointer variable, Pointer Arithmetic, Passing parameters by reference, Pointer to pointer, Pointer to functions KIIT UNIVERSITY

2 What the declaration tells the C compiler?
Introduction int i = 10; What the declaration tells the C compiler? Reserve space in memory to hold the integer value Associate the name i with this memory location Store the value 10 at this location Memory Map Code Snippet #include <stdio.h> int main() { int i = 10; printf(“Address of intVariable = %d”, &i); printf(“Value of intVariable = %d”, i); return 0; } i Location Name 10 Value at Location XXXXX (e.g. 6458) Location Number A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is − type *var-name; KIIT UNIVERSITY

3 Introduction cont… A pointer is a variable that holds a memory address. This address is the location of another entity (typically another variable) in memory. For example, if one variable contains the address of another variable, the first variable is said to point to the second. KIIT UNIVERSITY

4 Introduction cont… KIIT UNIVERSITY Code Snippet 1 Code Snippet 2
int main() { int i = 10; int *j; //‘*’ stands for value at address, also called indirection operator. j = &i; return 0; } int main() { int i = 10, *j, **k; j = &i; k = &j; return 0; } Memory Map Memory Map i j i j k 10 6458 10 6458 7455 6458 7455 6458 7455 8452 KIIT UNIVERSITY

5 Pointers Examples #include<stdio.h> int main() { int i=3; printf(“Address of i=%d\n”,&i); printf(“Value of i=%d\n”,i); printf(“Value of i=%d\n”,*(&i)); return 0; } OUTPUT: Address of i=65524 Value of i=3 When, argument is passed using pointer, address of the memory location is passed instead of value and is called as call by reference. int main( ) { int a = 10, b = 20 ; swapr ( &a, &b ) ; printf ( "\na = %d b = %d", a, b ) ; return 0; } swapr( int *x, int *y ) int t ; t = *x ; *x = *y ; *y = t ; KIIT UNIVERSITY

6 Passing parameters by Reference
int main() { int radius ; float area, perimeter ; printf ( "\nEnter radius of a circle " ) ; scanf ( "%d", &radius ) ; areaperi ( radius, &area, &perimeter ) ; printf ( "Area = %f", area) ; printf ( "\nPerimeter = %f", perimeter ) ; } areaperi ( int r, float *a, float *p ) *a = 3.14 * r * r ; *p = 2 * 3.14 * r ; KIIT UNIVERSITY

7 NULL Pointers KIIT UNIVERSITY Code Snippet Null Pointer Checking
It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer. The NULL pointer is a constant with a value of zero defined in several standard libraries. Code Snippet # include <stdio.h> int main() { int *ptr = NULL; printf("The value of ptr is : %d\n", ptr ); return 0; } Null Pointer Checking if (ptr == null) or if (ptr <> null) If (ptr) or if ( !ptr) Commonly done mistakes int c, *pc; pc=c; /* pc is address whereas, c is not an address. */ *pc=&c; /* &c is address whereas, *pc is not an address. */ KIIT UNIVERSITY

8 Pointer Conversion KIIT UNIVERSITY
One type of pointer can be converted into another type of pointer. In C, it is permissible to assign a void * pointer to any other type of pointer. It is also permissible to assign any other type of pointer to a void * pointer. void *ptr; // ptr is declared as void pointer char cnum; int inum; float fnum; ptr = &cnum; // ptr has address of character data ptr = &inum; // ptr has address of integer data ptr = &fnum; // ptr has address of float data Except for void *, all other pointer conversions must be performed by using an explicit cast. KIIT UNIVERSITY

9 Pointer Conversion cont…
int main() { double x = 100.1, y; int *p; /* The next statement causes p (which is an integer pointer) to point to a double. */ p = (int *) &x; /* The next statement does not operate as expected. */ y = *p; /* attempt to assign y the value x through p */ /* The following statement won't output */ printf(''The (incorrect) value of x is: %f", y); return 0; } KIIT UNIVERSITY

10 Pointer Arithmetic KIIT UNIVERSITY
There are only two arithmetic operations that you can use on pointers: addition and subtraction. To understand what occurs in pointer arithmetic, let p1 be an integer pointer with a current value of Also, assume ints are 2 bytes long. After the expression p1++; p1 contains 2002, not The reason for this is that each time p1 is incremented, it will point to the next integer. The same is true of decrements. For example, assuming that p1 has the value 2000, the expression p1--; causes p1 to have the value let p3 be an float pointer with a current value of Also, floats are 4 bytes long. After the expression p1++; p1 contains 2004, not The reason for this is that each time p1 is incremented, it will point to the next float. KIIT UNIVERSITY

11 Pointer Arithmetic KIIT UNIVERSITY const int MAX = 3;
const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* let us have array address in pointer */ ptr = var; for ( i = 0; i < MAX; i++) printf("Address of var[%d] = %d\n", i, ptr ); printf("Value of var[%d] = %d\n", i, *ptr ); /* move to the next location */ ptr++; } return 0; Incrementing a pointer const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* let us have array address in pointer */ ptr = &var[MAX-1]; for ( i = MAX; i > 0; i--) printf("Address of var[%d] = %d\n", i-1, ptr ); printf("Value of var[%d] = %d\n", i-1, *ptr ); /* move to the previous location */ ptr--; } return 0; Decrementing a pointer KIIT UNIVERSITY

12 Pointer Arithmetic KIIT UNIVERSITY
Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2 points to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared. const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* let us have address of the first element in pointer */ ptr = var; i = 0; while ( ptr <= &var[MAX - 1] ) { printf("Address of var[%d] = %d\n", i, ptr ); printf("Value of var[%d] = %d\n", i, *ptr ); /* point to the next location */ ptr++; i++; } return 0; KIIT UNIVERSITY

13 Pointers and Arrays KIIT UNIVERSITY
int arr[4]; In arrays of C programming, name of the array always points to the first element of an array. Here, address of first element of an array is &arr[0]. Also, arr represents the address of the pointer where it is pointing. Hence, &arr[0] is equivalent to arr. Also, value inside the address &arr[0] and address arr are equal. Value in address &arr[0] is arr[0] and value in address arr is *arr. Hence, arr[0] is equivalent to *arr. &a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1). &a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2). &a[3] is equivalent to (a+3) AND, a[3] is equivalent to *(a+3). . &a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i). KIIT UNIVERSITY

14 Pointers and Arrays cont…
In C, you can declare an array and can use pointer to alter the data of an array. #include <stdio.h> int main() { int i,class[6],sum=0; printf("Enter 6 numbers:\n"); for(i=0;i<6;++i) scanf("%d",(class+i)); // (class+i) is equivalent to &class[i] sum += *(class+i); // *(class+i) is equivalent to class[i] } printf("Sum=%d",sum); return 0; KIIT UNIVERSITY

15 Passing 1-D array to function
In below example, we are passing the address of array’s element as an argument to the function, instead of their values and have use pointers for this. disp( int *num) { printf("%d ", *num); } int main() int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; for (int i=0; i<=10; i++) /* passing element’s address*/ disp (&arr[i]); return 0; Now we will see how to pass an entire array to a function. myfunc( int *var1, int var2) { int x; for(x=0; x<var2; x++) printf("Value of var_arr[%d] is: %d \n", x, *var1); /*increment pointer for next element fetch*/ var1++; } int main() int var_arr[] = {11, 22, 33, 44, 55, 66, 77}; myfunc(&var_arr, 7); return 0; KIIT UNIVERSITY

16 Passing 2-D array to function
Using a single pointer void print(int *arr, int m, int n) { int i, j; for (i = 0; i < m; i++) for (j = 0; j < n; j++) printf("%d ", *((arr+i*n) + j)); } int main() int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int m = 3, n = 3; print((int *)arr, m, n); return 0; Using array of pointers or double pointers // Same as "void print(int **arr, int m, int n)" void print(int *arr[], int m, int n) { int i, j; for (i = 0; i < m; i++) for (j = 0; j < n; j++) printf("%d ", *((arr+i*n) + j)); } int main() int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int m = 3, n = 3; print((int **)arr, m, n); return 0; KIIT UNIVERSITY

17 Arrays of Pointers KIIT UNIVERSITY #include <stdio.h>
const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i; for (i = 0; i < MAX; i++) printf("Value of var[%d] = %d\n", i, var[i] ); } return 0; There may be a situation when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer − int *ptr[MAX]; It declares ptr as an array of MAX integer pointers. Thus, each element in ptr, holds a pointer to an int value. The following example uses three integers, which are stored in an array of pointers, as follows const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr[MAX]; for ( i = 0; i < MAX; i++) ptr[i] = &var[i]; /* assign the address of integer. */ } printf("Value of var[%d] = %d\n", i, *ptr[i]); return 0; KIIT UNIVERSITY

18 Pointer to Pointer KIIT UNIVERSITY
A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below. A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. For example, the following declaration declares a pointer to a pointer of type int − int **var; When a target value is indirectly pointed to by a pointer to a pointer, accessing that value requires that the asterisk operator be applied twice, as is shown below - int var, int *ptr; int **pptr; var = 3000; /* take the address of var */ ptr = &var; /* take the address of ptr using address of operator & */ pptr = &ptr; KIIT UNIVERSITY

19 Pointers to Function KIIT UNIVERSITY
Pointers can also point to C function. Let’s explore with an example void display(); int main() { printf(“\nAddress of function display is %d”, display); display(); return 0; } void display() printf(“\nLong Live all of you”); To obtain the address of a function, name of the function is to be mentioned. Now look at in invoking a function using a pointer to a function KIIT UNIVERSITY

20 Invoking a function using a pointer to a function
void display(); int main() { void (*funct_ptr)(); funct_ptr = display; /* assign address of function */ printf(“\nAddress of function display is %d”, funct_ptr); (*funct_ptr)(); /* invokes the function display */ return 0; } void display() printf(“\nLong Live all of you”); KIIT UNIVERSITY

21 Function returning Pointer
The way functions return an int, a float or any other data type, it can even return a pointer. Let’s look at the following example … int *fun(); int main() { int *p; p = fun(); return 0; } int *fun() static int i = 20; return (&i); KIIT UNIVERSITY

22 Target spot-on? KIIT UNIVERSITY

23 Thank You KIIT UNIVERSITY

24 Home Work (HW) KIIT UNIVERSITY
Twenty-five numbers are entered from the keyboard into an array. The number to be searched is entered through the keyboard by the user. WAP to find if the number to be searched is present in the array and if it is present, display the number of times it appears in the array. Twenty-five numbers are entered from the keyboard into an array. WAP to find out how many of them are positive, how many are negative, how many are even and how many odd. WAP to find the smallest number in an array WAP to copy the contents of one array into another in the reverse order. WAP to find if a square matrix is symmetric. WAP to obtain the determinant value of a 5 x 5 matrix. WAP to add two 6 x 6 matrices. WAP to multiply any two 3 x 3 matrices WAP to pick up the largest number from any 5 row by 5 column matrix. KIIT UNIVERSITY


Download ppt "Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area"

Similar presentations


Ads by Google