Pointers Introduction to Pointers Call by value and call by reference
1. An Introduction to Pointers A pointer can be used to store the memory address of other variables, functions, or even other pointers. The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C . As the pointers store the memory addresses, their size is independent of the type of data they are pointing to.
Syntax Datatype * ptr ; where ptr is the name of the pointer. datatype is the type of data it is pointing to.
Pointer Declaration In pointer declaration, only declares the pointer but do not initialize it. To declare a pointer ( * ) dereference operator is used before its name . Example: int * ptr ;
Pointer Initialization Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the ( & ) addressof operator to get the memory address of a variable and then store it in the pointer variable . Example int var = 50 ; int * ptr ; ptr = & var ; Or in a single step we can declare and initialize as follows int * ptr = & var ;
Dereferencing Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration.
Example Void main() { int i = 5; int * ptr ; ptr = & i ; printf (“ i = %d\n”, i ); Printf (“& i = %p\n”, & i ); printf (“* ptr = %d\n”, * ptr ); printf (“ ptr = %p\n”, ptr ); g etch (); } Output: i = 5 & i = affed0 * ptr = 5 ptr = effff5e0
2. Call by value and call by reference Call By Value In call by value method of parameter passing, the values of actual parameters are copied to the function’s formal parameters. There are two copies of parameters stored in different memory locations. One is the original copy and the other is the function copy. Any changes made inside functions are not reflected in the actual parameters of the caller.
Example of Call by Value // C program to illustrate call by value #include < stdio.h > // Function Prototype void swapx (int x, int y); // Main function int main() { int a = 10, b = 20; // Pass by Values swapx (a, b); printf ("In the Caller:\ na = %d b = %d\n", a, b); return 0; } // Swap functions that swaps // two values void swapx (int x, int y) { int t; t = x; x = y; y = t; printf ("Inside Function:\ nx = %d y = %d\n", x, y); } Output Inside Function : x = 20 y = 10 In the Caller: a = 10 b = 20 Thus actual values of a and b remain unchanged even after exchanging the values of x and y in the function.
Call by Reference In call by reference method of parameter passing, the address of the actual parameters is passed to the function as the formal parameters. Both the actual and formal parameters refer to the same locations. Any changes made inside the function are actually reflected in the actual parameters of the caller.
Example of Call by Reference // C program to illustrate Call by Reference #include < stdio.h > // Function Prototype void swapx (int*, int*); // Main function int main() { int a = 10, b = 20; // Pass reference swapx (&a, &b); printf ("Inside the Caller:\ na = %d b = %d\n", a, b); return 0; } // Function to swap two variables // by references void swapx (int* x, int* y) { int t; t = *x; *x = *y; *y = t; printf ("Inside the Function:\ nx = %d y = %d\n", *x, *y); } Output Inside the Function: x = 20 y = 10 Inside the Caller: a = 20 b = 10 Thus actual values of a and b get changed after exchanging values of x and y.