Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Variables and memory addresses
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Lecture 5 Pointers 1. Variable, memory location, address, value
Arithmetic Expressions
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
User-Written Functions
Chapter 8 Arrays, Strings and Pointers
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Week 9 - Pointers.
UNIT 5 C Pointers.
BILASPUR UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE AND APPLICATION
Functions and Pointers
POINTERS.
Tokens in C Keywords Identifiers Constants
Pointers and Pointer-Based Strings
Pointers.
CNG 140 C Programming (Lecture set 10)
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Arrays in C.
POINTERS.
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
Pointers.
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Tejalal Choudhary “C Programming from Scratch” Pointers
Visit for more Learning Resources
C Passing arrays to a Function
Arrays, For loop While loop Do while loop
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Pointers.
Lectures on Numerical Methods
Outline Defining and using Pointers Operations on pointers
Pointers.
Objectives You should be able to describe: Addresses and Pointers
Pointer Operations.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
C (and C++) Pointers April 4, 2019.
Pointers and Pointer-Based Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
POINTERS.
Data Structures and Algorithms Introduction to Pointers
EECE.2160 ECE Application Programming
Exercise Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Introduction to C Programming
Introduction to Pointers
Presentation transcript:

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

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

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

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

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

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

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

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

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 100.1. */ printf(''The (incorrect) value of x is: %f", y); return 0; } KIIT UNIVERSITY

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 2000. Also, assume ints are 2 bytes long. After the expression p1++; p1 contains 2002, not 2001. 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 1998. let p3 be an float pointer with a current value of 2000. Also, floats are 4 bytes long. After the expression p1++; p1 contains 2004, not 2001. The reason for this is that each time p1 is incremented, it will point to the next float. KIIT UNIVERSITY

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

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

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

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

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

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

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

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

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

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

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

Target spot-on? KIIT UNIVERSITY

Thank You KIIT UNIVERSITY

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