Check The Programming Section
Pointer varibales in C programming is used for specific purpose and used to point to a memory block by holding its address. A memory block three properties, as follows:
name (as num)
value (as 25) and
an address (as 2000)
A pointer is always declared as same type of a variable type. So that pointer can hold the address of that type variable. For example,
int *ptr;
int num = 25;
ptr = #
In the above example, ptr is declared as pointer variable, which can point a memory block of type integer variable. In this example, num is declared as integer and its address (&num) is assigned to the pointer variable. Like this we can use two pointer variables to hold the address of two variables to interchange the values of two variables. The required code is as follows:
#include<stdio.h>
void swap(int *ptr1, int *ptr2){
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
int main(){
int num1;
int num2;
printf("Enter two values::");
scanf("%d %d",&num1,&num2);
printf("Values before swapping....\n");
printf("num1 = %d, num2 = %d\n",num1, num2);
swap(&num1, &num2);
printf("Values after swapping.....\n");
printf("num1 = %d, num2 = %d",num1,num2);
return 0;
}
So, the pointer variable has the value of the address of num and it also means ptr is pointing to num. In the above program, address of num1 and num2 is passed as argument to the function swap. To save their address two pointer varibales *ptr1 and *ptr2 is declared as the argument to the function swap. Most importantly the type of num1 and num2 is same as the type of ptr1 and ptr2.
Before Swapping
During Swapping