Pointers are symbolic representation of addresses.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
Syntax:
type *var-name;
1.WAP to program to illustrate Pointers in C.
#include <stdio.h> void main() { int var = 20; // declare pointer variable int *ptr; // note that data type of ptr and var must be same ptr = &var; // assign the address of a variable to a pointer printf("Value at ptr = %p \n",ptr); printf("Value at var = %d \n",var); printf("Value at *ptr = %d \n", *ptr); }
Output:
We can use the Pointer by two method:
1.Call By Value: Passing a Copy of Variable
2.Call By Reference: Passing a address of Variable.
1.Call By Value:
In this method we pass a copy of the variable and not the actual variable to the called function. So, any change made to the copy of the variable in the called function is not reflected back to the original variable.
Example:
{
int *p; //pointer Initilization
int c;
int a[3]={1,25,3,4};
c = &a[0]; // Accessing the value of array a with index 0
p=a+1;
printf("%d",*p); // Displaying value at pointer p
}
0 Comments