Basics of Pointer in C Language - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Basics of Pointer in C Language

Share This
A pointer is a special type of variable that holds the address of another variable. A pointer variable does not create a copy of a variable. It stores only the address of the variable. Hence, if the value of the variable is changed, using the pointer variable we get the updated value.

A pointer is a variable, hence a pointer pointing to a variable can be used to point to another variable later. In this tutorial, we will discuss the pointer with a very simple example. The following code presents how a pointer variable is declared that can store an address of an integer variable. 

int *ptr;
Here is the complete code.
#include <stdio.h> int main (void) { int x; int *ptr; int **c; x = 11; ptr = &x; c = &ptr; printf("\n x = %d", x); printf("\n &x = %d", &x); x++; printf("\n ptr = %d", ptr); printf("\n &ptr = %d", &ptr); printf("\n *ptr = %d", *ptr); x++; printf("\n c = %d", c); printf("\n &c = %d", &c); printf("\n *c = %d", *c); printf("\n **c = %d", **c); printf("\n"); return 0; }
Accessing different variables using one Pointer
#include <stdio.h> int main() { int x = 10; int y = 20; int *ptr; printf("\n%d", &x); printf("\n%d", &y); ptr = &x; printf("\n%d", ptr); printf("\n%d", *ptr); ptr = &y; printf("\n%d", ptr); printf("\n%d", *ptr); printf("\n"); return 0; }
Now Compile and run the program.
#cc filename.c
#./a.out
Memory allocation for pointer variable is not dependent on data types
#include <stdio.h> int main (void) { int *ptri; char *ptrc; float *ptrf; printf("%d %d %d \n", sizeof(ptri), sizeof(ptrc), sizeof(ptrf)); return 0; }
Swapping numbers using Pointer
Here, we are trying to swap numbers using a function. So, to perform the swap operation, a code is written below. In this example, values of x and y variables are passed in the swap function and those are received by variables a and b, respectively. After swapping the numbers, we have printed the result in the swap as well as in the main function. Assume, that the value of x is 10 and y is 12. So, in the swap function, the print statement gives the result as x = 12 and y = 10. But, in the main function, the print statement shows x = 10 and y = 12.

#include <stdio.h> void swap (int, int); int main (void) { int x, y; printf("Enter value of x and y :"); scanf("%d %d", &x, &y); swap(x, y); printf("x = %d and y = %d\n", x, y); return 0; } void swap (int x, int y) { int temp; temp = x; x = y; y = temp; printf("x = %d and y = %d\n", x, y); }
Hence, the problem is that though we have called the swap function, it is not working on the data of the main function. The reason behind this is when a function is called, the values are copied into local variables in the called function. So, the values are swapped in the swap function, but it not reflected in the caller function. In this context, the solution to the problem is instead of passing the value of the variables we can pass the addresses of the variables to the called function. Hence, any changes made to those data are being reflected in the caller function.

#include <stdio.h> void swap (int *, int *); int main (void) { int x, y; printf("Enter value of x and y :"); scanf("%d %d", &x, &y); swap(&x, &y); printf("x = %d and y = %d", x, y); return 0; } void swap (int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; }


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.