Unit - 4.2 - Pointers - PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides a foundational explanation about pointers and memory allocation in the C programming language. The document covers basic concepts of declaring and initializing pointers, as well as techniques like "Dynamic Memory Allocation" and how memory is allocated in a C program.
Full Transcript
Unit - 4 Arrays and Pointers Topic to be Covered Arrays: ❖ Arrays (1-D, 2-D) ❖ Character arrays and strings Pointers: ❖ Idea of pointers ❖ Defining pointers ❖ Use of Pointers in self-referential structure...
Unit - 4 Arrays and Pointers Topic to be Covered Arrays: ❖ Arrays (1-D, 2-D) ❖ Character arrays and strings Pointers: ❖ Idea of pointers ❖ Defining pointers ❖ Use of Pointers in self-referential structures ❖ Notation of linked list (no implementation) ❖ Dynamic Memory allocation Functions Idea of pointers ❖ As you know, every variable is a memory location and every memory location has its address defined which can be accessed using the ampersand (&) operator, which denotes an address in memory. ❖ C pointer is the derived data type that is used to store the address of another variable and can also be used to access and manipulate the variable's data stored at that location. The pointers are considered as derived data types. ❖ With pointers, we can access and modify the data located in the memory, pass the data efficiently between the functions, and create dynamic data structures like linked lists, trees, and graphs. Pointer Declaration ❖ To declare a pointer, use the dereferencing operator (*) followed by the data type. Syntax The general form of a pointer variable declaration is : type *var-name; ❖ Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable. ❖ The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Idea of Pointer Example of Valid Pointer Variable Declarations int *ip; double *dp; float *fp; char *ch Pointer Initialization ❖ After declaring a pointer variable, you need to initialize it with the address of another variable using the address of (&) operator. This process is known as referencing a pointer. Syntax The following is the syntax to initialize a pointer variable – pointer_variable = &variable; Example Here is an example of pointer initialization – int x = 10; int *ptr = &x; Defining Pointer Referencing and Dereferencing Pointers ❖ A pointer references a location in memory. Obtaining the value stored at that location is known as dereferencing the pointer. ❖ In C, it is important to understand the purpose of the following two operators in the context of pointer mechanism − ❖ The & Operator − It is also known as the "Address-of operator". It is used for Referencing which means taking the address of an existing variable (using &) to set a pointer variable. ❖ The * Operator − It is also known as the "dereference operator". Dereferencing a pointer is carried out using the * operator to get the value from the memory address that is pointed by the pointer. Example #include #include int main() { int number=50; int *p; p=&number; printf("Address of variable number is %x \n", p); printf("Value of p variable is %d \n",*p); printf("Address of variable p = %u\n", &ptr) ; return 0; } Example #include int main() { int x = 10; float y = 1.3f; char z = 'p'; // Pointer declaration and initialization int * ptr_x = & x; float * ptr_y = & y; char * ptr_z = & z; // Printing the values printf("Value of x = %d\n", *ptr_x); printf("Value of y = %f\n", *ptr_y); printf("Value of z = %c\n", *ptr_z); printf("Value of x = %p\n", ptr_x); printf("Value of y = %p\n", ptr_y); printf("Value of z = %p\n", ptr_z); return 0; } sizeof() Pointer #include #include int main() { int x = 10; float y = 1.3f; char z = 'p'; int *ptr_x = & x; float *ptr_y = & y; char *ptr_z = & z; printf("Size of integer pointer : %lu\n", sizeof(ptr_x)); printf("Size of float pointer : %lu\n", sizeof(ptr_y)); printf("Size of char pointer : %lu\n", sizeof(ptr_z)); return 0; } ACCESSING A VARIABLE THROUGH ITS POINTER #include #include ❖ Once a pointer has been void main() assigned the address of a { variable, the value of the int x, y; variable can be accessed using int *ptr; the unary operator asterisk (*). x = 10; ptr = &x; y = *ptr; printf(“Value of x is %d\n\n”,x); printf(“%d”,x); printf(“%d”,y); *ptr = 25; printf(“\nNow x = %d\n”,x); } Pointer Arithmetic in C ❖ Pointer variables are used to store the #include address of variables. #include ❖ Address of any variable is an unsigned void main() integer value i.e., it is a numerical value. { int a, *intPtr ; ❖ So we can perform arithmetic operations on float b, *floatPtr ; pointer values. double c, *doublePtr ; ❖ But when we perform arithmetic operations clrscr() ; on pointer variable, the result depends on intPtr = &a ; // Asume address of a is 1000 the amount of memory required by the floatPtr = &b ; // Asume address of b is 2000 variable to which the pointer is pointing. doublePtr = &c ; // Asume address of c is 3000 Address At Pointer+(Number To Be Add * Bytes Of intPtr = intPtr + 3 ; // intPtr = 1000 + ( 3 * 2 ) Memory Required By Datatype ) floatPtr = floatPtr - 2 ; // floatPtr = 2000 - ( 2 * 4 ) doublePtr++ // doublePtr = 3000 + 8 ❖ Following arithmetic operations are possible on the pointer in C language: printf("intPtr value : %u\n", intPtr) ; 1. Addition printf("floatPtr value : %u\n", floatPtr) ; 2. Subtraction printf("doublePtr value : %u", doublePtr) ; 3. Increment 4. Decrement getch() ; 5. Comparison } Chain of Pointers (or) Pointer to Pointer ❖ In the c programming language, we have pointers to store the address of variables of any datatype. A pointer variable can #include store the address of a normal variable. C programming language also provides a pointer variable to store the address of another pointer variable. This type of pointer variable is #include called a pointer to pointer variable. Sometimes we also call it a double pointer. void main( ) { int x, *p1, **p2; ❖ Here, the pointer variable p2 contains the address of the pointer variable p1, which points to the location that contains the desired value. This is known as multiple indirections. x = 100; ❖ A variable that is a pointer to a pointer must be declared p1 = &x; using additional indirection operator symbols in front of the name. p2 = &p1; printf (“%p\t %p\n”, p1, p2); printf (“%d\t %d”,*p,**p2); ❖ Syntax: datatype **pointerName ; } Pointers and Arrays in C ❖ When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations. ❖ The base address is the location of the first element(index 0) of the array. ❖ Suppose we declare an array x as follows: int x = {1, 2, 3, 4, 5}; ❖ Suppose the base address of x is 1000 and assuming that each integer requires two bytes, the five elements will be stored as follows: Pointers and Arrays in C #include #include ❖ The name x is pointing to a first element x, int main() (i.e ) x = &x { int arr = {1, 2, 3, 4, 5}; ❖ If we declare p as an integer pointer, then we can int *ptr = arr; make the pointer p to point to the array x by the printf(“%d”,*ptr); following assignment printf(“%d”,*(ptr+1)); p = x; printf(“%d”,*(ptr+2)); printf(“%d”,*(ptr+3)); This is equivalent to printf(“%d”,*(ptr+4)); p = &x; return 0; } ❖ Now, we can access every value of x between p and x using p++ to move from one element to #include another. #include int main() { int arr = {1, 2, 3, 4, 5}; int *ptr = arr; printf(“%d”,*(ptr+1)); printf(“%d”,*(ptr-1)); printf(“%d”,*(ptr-2)); printf(“%d”,*(ptr-3)); return 0; } Pointers and Arrays in C #include #include int main() { int arr = {1, 2, 3, 4, 5}; int *b = arr; int i; for(i = 0; i