CS101 Chapter 6-GK (1) PDF
Document Details
Uploaded by AdequateMulberryTree
Mediterranean Institute of Technology
Tags
Summary
This document is a lecture on pointers in C programming. It covers topics like pointer declaration, initialization, use cases, and benefits of pointers. This is part of the CS101 Introduction to Programming course, from the MEDITERRANEAN INSTIUTE OF TECHNOLOGY.
Full Transcript
CS101 INTRODUCTION TO PROGRAMMING 12/02/2024 1 WORKING WITH POINTERS Lecture 6 12/02/2024 2 Outlines Pointers Pointers for Inter‐Function Communication 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOG...
CS101 INTRODUCTION TO PROGRAMMING 12/02/2024 1 WORKING WITH POINTERS Lecture 6 12/02/2024 2 Outlines Pointers Pointers for Inter‐Function Communication 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 3 Pointers A pointer is a variable that stores/hold address of another variable of same data type. It is also known as locator or indicator that points to an address of a value. A pointer is a derived data type in C Benefit of using pointers: Pointers are more efficient in handling Arrays and Structures. Pointer allows references to function and thereby helps in passing of function as arguments to other function. It reduces length and the program execution time. It allows C to support dynamic memory management. 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 4 Pointers: Declaration Vs Initialization Declaration of Pointer: data_type * pointer_variable_name; int *p; Note: The * operator is called the dereference operator when used with a pointer. Note: If you have a pointer p that holds the address of a variable, the expression *p gives you access to the value stored at that address. 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 5 Pointers: Initialization of Pointer variable Pointer Initialization is the process of assigning an address of a variable to a pointer variable. The & operator is the address-of operator in C. the expression &var gives you the address in memory where var is stored. int a = 10 ; int * ptr ; //pointer declaration ptr = &a ; //pointer initialization contains the address of a or, int * ptr = &a ; //initialization and declaration together Note: Pointer variable always points to same type of data. float a; int * ptr; ptr = &a; //ERROR, type mismatch 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 6 Pointers Pointer example In the above figure, pointer variable stores the address of number variable i.e. fff4. The value of number variable is 50. But the address of pointer variable p is aaa3. By the help of *(indirection operator), we can print the value of pointer variable p. 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 7 Pointers: Summary Reference operator (&) and Dereference operator (*) & is called reference operator. It gives you the address of a variable. (*) is called the dereference operator that gets the value from the address. Symbols used in pointer 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 8 Pointers: Dereferencing of Pointer: Once a pointer has been assigned the address of a variable, to access the value of variable, pointer is dereferenced, using the indirection operator *. int main() { int a; int *p= NULL; a = 10; p = &a; printf("The value of a is:%d",*p); printf("\nThe value of a is:%d",*&a); printf("\nThe address of a with %%u format is:%u",&a); printf("\nThe address of a with %%x format is:%x",&a); printf("\nThe address of a with %%p format is:%p",&a); printf("\nThe address of a is:%p",p); printf("\nThe address of p is:%p",&p); return 0; } Note: %u is used to print an unsigned integer in decimal notation. %x is used to print an unsigned integer in hexadecimal notation. 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 9 Pointers: Dereferencing of Pointer: Once a pointer has been assigned the address of a variable, to access the value of variable, pointer is dereferenced, using the indirection operator *. int main() { int a; int *p= NULL; a = 10; p = &a; printf("The value of a is:%d",*p); //this will print the value of a. printf("\nThe value of a is:%d",*&a); //this will also print the value of a. printf("\nThe address of a with %%u format is:%u",&a); //this will print the address of a. printf("\nThe address of a with %%x format is:%x",&a); //this will print the address of a. printf("\nThe address of a with %%p format is:%p",&a); //this will print the address of a. printf("\nThe address of a is:%p",p); //this will also print the address of a. printf("\nThe address of p is:%p",&p); //this will also print the address of p. return 0; } 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 10 Pointers KEY POINTS TO REMEMBER ABOUT POINTERS IN C: Normal variable stores the value whereas pointer variable stores the address of the variable. The content of the C pointer always be a whole number i.e. address. C pointer is initialized to null, i.e. int *p = null. The value of null pointer is 0. & symbol is used to get the address of the variable. * symbol is used to get the value of the variable that the pointer is pointing to. If a pointer in C is assigned to NULL, it means it is pointing to nothing. Two pointers can be subtracted to know how many elements are available between these two pointers. Pointer addition, multiplication, division are not allowed. 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 11 Pointers: Practice #include int main() { int * ptr, q; q = 50; ptr = &q; printf("%d", *ptr); return 0; } 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 12 Pointers: Solution #include int main() Output { 50 int *ptr, q; q = 50; ptr = &q; printf("%d", *ptr); return 0; } 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 13 Pointers: Example 1 #include #include Output void main(){ :? int number=50; int *p; p=&number; printf("%p \n",&number); printf(“%p \n", p); printf("%d \n",*p); getch(); } 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 14 Pointers: Example 2 #include int main() { int var =10; int *p; p= &var; printf ( "\n Address of var is: %p", ); printf ( "\n Address of var using p is: %p", ); printf ( "\n Address of pointer p is: %u", ); printf( "\n Value of pointer p is: %u", ); printf ( "\n Value of var using var is: %d", ); printf ( "\n Value of var using p is: %d", ); printf ( "\n Value of var using the address of var is: %d", ); } 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 15 Pointers: Example 2 #include int main() { int var =10; int *p; p= &var; printf ( "\n Address of var is: %u", &var); printf ( "\n Address of var is: %p", p); printf ( "\n Address of pointer p is: %u", &p); printf( "\n Value of pointer p is: %u", p); printf ( "\n Value of var is: %d", var); printf ( "\n Value of var is: %d", *p); printf ( "\n Value of var is: %d", *&var); } 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 16 Pointers: Solution Output: #include Address of var is: 00XBBA77 int main() Address of var is: 00XBBA77 { Address of pointer p is: int var =10; 77221111 int *p; Value of pointer p is: p= &var; 00XBBA77 printf ( "\n Address of var is: %p", &var); Value of var is: 10 printf ( "\n Address of var is: %p", p); Value of var is: 10 printf ( "\n Address of pointer p is: %p", &p); Value of var is: 10 printf( "\n Value of pointer p is: %p", p); printf ( "\n Value of var is: %d", var); printf ( "\n Value of var is: %d", *p); printf ( "\n Value of var is: %d", *&var); } 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 17 Pointers: Practice Write a C program that accomplishes the following tasks: 1. Declare an integer variable Number and initialize it with a value. 2. Declare a pointer variable point that is meant to point to the integer Number. 3. Print the value of Number using both the variable and the pointer. 4. Find 2 ways to print the address of Number. 5. Increment the value of number by 10 using the pointer point. 6. Print the new value of Number using both the variable and the pointer. 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 18 Practice: Solution int main() { int Number = 42;// Declare an integer variable number int *point = &Number;// Declare a pointer variable point that point to number. // Print the value of number using both the variable and the pointer. printf("The value of number using the variable: %d\n", Number); printf("The value of number using the pointer: %d\n", *point); printf("The value of point is : %p\n", point); printf("The address of Number is : %p\n", &Number); // Increment the value of number by 10 using the pointer point. *point += 10; // Print the new value of number using both the variable and the pointer. printf("The new value of number using the variable: %d\n", Number); printf("The new value of number using the pointer: %d\n", *point); printf("The new value of point is : %p\n", point); printf("The new address of number is : %p\n", &Number); return 0; } 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 19 Pointers: Exercises 1. Write a C program to read two numbers from user and find the sum, substruction, multiplication and division of two number using pointers in C programming. Input Input num1: 10 Input num2: 20 Output Sum = 30 Difference = -10 Product = 200 Quotient = 0 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 20 Pointers 2. Write a program in C to find the maximum number between two numbers using a pointer. Test Data : Input the first number : 5 Input the second number : 6 Expected Output : 6 is the maximum number 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 21 Pointers for Inter‐Function Communication Arrays and Pointers When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array. Base address which gives location of the first element is also allocated by the compiler. Suppose we declare an array arr: int arr={ 1, 2, 3, 4, 5 }; Assuming that the base address of arr is 1000 and each integer requires two byte, the five element will be stored as follows 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 22 Pointers for Inter‐Function Communication Here variable arr will give the base address, which is a constant pointer pointing to the element, arr. Therefore, arr is containing the address of arr i.e 1000. arr is equal to &arr // by default We can declare a pointer of type int to point to the array arr. int arr={ 1, 2, 3, 4, 5 }; int *p; p = arr; or p = &arr; //both the statements are equivalent Now we can access every element of array arr using p++ to move from one element to another. NOTE : You cannot decrement a pointer once incremented. p-- won't work 12/02/2024 MEDITERRANEAN INSTIUTE OF TECHNOLOGY 23 Pointers for Inter‐Function Communication Pointer to Array We can use a pointer to point to an Array, and then we can use that pointer to access the array. Example1: Output int i; 12345 int a = {1, 2, 3, 4, 5}; int* p = a; // same as int*p = &a for (i=0; i