How to use Pointers in C language?Pointers are like normal variables, but in the place of storing variable value, pointers store the address of another variable or another pointer. A pointer can hold the addresses of variable of different data types-integer, char, or even an array. When a pointer is holding an address of a variable, we say that the pointer is pointing to that variable. This article explains the basics of pointers for beginner-coders. Syntax:Points to understand:
![]() Example: Here, p is a pointer of data type integer.
Example: Warning: warning: assignment to 'int *' from incompatible pointer type 'float * Important points:
Simple example program:Output: Value of b =50 Address of b =6487572 Address of ptr =6487576 Value of ptr =50 The pointer stores =6487572 As you can see, the value of b and the value of *ptr are the same, as ptr stores the value of b. The address of the variable b is the same as the value in the pointer. The address of the pointer is independent. ![]() Referencing and Dereferencing operators:Referencing: It is taking the address of an entity using "&." Dereferencing takes the value at operator "*" to access the value at the pointer.
![]() Arithmetic on PointersSuppose, an integer pointer is pointing to a variable at address 500:
Remember that 4 is the size of the integer data type. Another example: Code: Output: ![]() Pointer to pointer: As mentioned in the definition, a pointer can store the address of another pointer, and then it is called a double-pointer. It is also known as pointer-to-pointer. ![]() Syntax: Example program: Output: a = 65 address of a = 6487572 ptr1 = 6487572 address ptr1 = 6487560 *ptr1 = 65 ptr2 = 6487560 *ptr2 = 6487572 **ptr2 = 65 Pointer to Arrays
![]() Here, ptr is the pointer pointing to the first element of the array (arr[0]) Points to understand: For an array: int arr[4], a pointer say ptr is pointing at arr[0], then:
![]() Example program: Output: Enter 6 numbers: 2 3 4 5 6 0 Sum = 20 Understanding: Here, x[6] is the array to which by default x is the pointer pointing to its base address. So, instead of x[i], we can say x+i as mentioned above. Using our own pointer to array: Output: *ptr = 3 *(ptr+1) = 4 *(ptr-1) = 2 Pointer as a Function argumentPre-requisite: call by value (functions) To pass arguments in the function call to the function declaration, we can either use call by value in which we pass the values, and here is the second and more efficient way- Call by Reference, where we pass the addresses to the pointers. Example program: Output: a = 10 b = 20 *m and *n = 10 and 20 Now, *m = 20 and *n = 10 After Swapping: a = 20 b = 10 Understanding: Here, we defined a function "swap" to swap the values of two variables, "a" and "b". We gave the values 10 and 20 to "a" and "b," respectively. We defined the function with two pointers, *m and *n. Now, we pass the addresses of these two arguments to the pointer parameters. So, in the function call, we pass "&a" and "&b" to *m and *n in the declaration. Now, we swap the values using *(value at) pointers.
This can be observed in the function declaration. The values of *m and *n are also modified along "a" and "b". Pointer to StructurePre-requisite: Structures in C When we want to access a member in a structure using its variable, we use the "." operator. If we declare a pointer of structure type, then we use the "->" operator to access the members of the structure. Example: Here, employeePtr -> age is equivalent to (*employeePtr).age employeePtr -> weight is equivalent to (*employeePtr).weight Example program: Output: NAME: Rishab NUMBER: 100 RANK: 1 Self Referential StructureThese are the structures that consist of one or more pointers that point to the same structure as their members. ![]() To make it clear, Structures that point to the same type of structures are "Self-referential structures". Example program: Output: 30 40 Features of Pointers
Interview questions on Pointers1. What do you think will be the output of this program? Solution: On execution, this program gives a compile-time error. error: invalid use of void expression This is because "vptr" is a void pointer. Void pointers cannot be dereferenced. If we want to, then we need to typecast it and then do the dereferencing. Void pointer: The pointer without any data type associated with it. 2. What will be the output of this program? Answer: Here, the variable k is a double pointer pointing to "i" indirectly. But, k is incremented. After incrementing, this final value is some garbage value that is not the address of any other variable. Hence, It could result in a segmentation fault or a garbage value being printed. 3. What will the output of this program be? Answer: Here, the pointer j is pointing to i. But, in the next step, j is incremented. Now, j neither has any address nor any value. Rather, it has a garbage value stored in it as we did not initialize anything to the address next to the address of the variable "i". 4. What are the differences between call by value and call by reference?
5. What are the most significant uses of pointers in any programming language? Answer: Pointers are used in many scenarios. But, out of those many uses, some of the significant uses are:
Next TopicWhat is a Storage Class in C
|