POINTER

  • Pointer are variable that are used to store the address of another variable
  • address of a variable is the memory location number which is allocated to the variable. the memory address are 0,1,2,3….and so on up to the capacity of the memory
  • pointer in c unlike other variable do not store values. As stated they store the address of other variables
  • It is already mentioned in the first statement that pointers are also variables. Hence we can also have a pointer that is pointing to another pointer
  • Pointer in c have been lumped with the goto statement as a marvelous way to create impossible-to-understand programs
  • This is certainly true when they are used carelessly, and it is easy to create a pointer that point somewhere unexpected
  • With the discipline, however, the pointer can also be used to achieve clarity and simplicity
  • Similarly ;when the value of “i” is “1”, the location pointed by *(a+i) and *(i+a) is the second location of the array
Pointer in C
Pointer in C

assume that variable 1,2,3,4,5 are the human beings and they lived at the location a[0],a[1],a[2],a[3],a[4],a[5] so how can you call them ? .By knowing the address of the human beings this can happen only if they have id proof which defines their location so we can go to their location and find the individual one.

Call by value method:

*, In this case, the value of the parameter is passed to the called function
*, In this case, the actual parameters are not accessible by the called function
*Hence the actual parameter remains unchanged in case of the call by value.

Call by reference method:

*, In this case, the reference of the variable is passed to the function by passing the address of the parameters
*In this case, since the address of the variables are available, the called function can access the actual parameters
*Hence the actual parameter can be altered if required in case of the call by the reference method.

Swap two number using a call by reference method

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
void swap(int *p1,int *p2);
clrscr();
printf("Enter two number: ");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("the value of a and b in main function after calling the swap function are %d and %d\n"a,b);
getch();
}
void swap(int *p1,int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
printf("the value of a and b in the swap function after swapping are %d and %d\n",*p1,*p2);
}
OUTPUT:
Enter two number:4
5
the value of a and b in the swap function after swapping are 5 and 4
the value of a and b in main function after calling the swap function are 5 and 4
Translate »