What are Pointers?
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. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations −
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to.
How to Use Pointers?
There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. The following example makes use of these operations −
Pointer variables
In C, there is a special variable that stores just the address of another variable. It is called Pointer variable or, simply, a pointer.
Declaration of Pointer
data_type* pointer_variable_name;
int* p;
EXAMPLE PROGRAM FOR POINTERS IN C:
#include
#include
int main()
{
int *ptr, q;
q = 100;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
POINTERS IN C – PROGRAM OUTPUT:
100
NULL Pointers
It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program −
#include
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}
Output :
The value of ptr is 0.
-In most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.
To check for a null pointer, you can use an 'if' statement as follows −
if(ptr) /*succeeds if p is not null*/
if(!ptr) /*succeds if p is null*/
void Pointers:
we can declare a pointrer to be a of any data type void any cannot hold any value.so we have to type cast the pointer variable from one type to other type.
int x=27;
void *p;
p=&x;
printf("%d",(((int*)p));
Output :
The statement (int*)p makes p to become an interger type
Example:
#include stdio.h
#include conio.h
main()
{
int x=27;
void*p;
p=&x;
printf("value is=%d",*((int *)p));
}
Output:
Value is 27
Pointer and Functions
1.Pointers can be passed as arguments to a functions definition.
2.Passing pointer to a functions is called call by value.
3.In call by reference what ever changes are made to formal arguments of the functions definition will affect the actual arguments in calling functions.
4.In call by reference the actual arguments must be pointers or references or addresses.
5.Pointer arguments are use full in functions,because they allow accessing the orginal data in the calling program.
Ex1 :
void swap(int*,int*);
main()
{
int a=10,b=20;
swap(&a,&b);
printf("%d,%d",a,b);
}
void swap(int*x,int*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Output: 20 10
we can declare a pointrer to be a of any data type void any cannot hold any value.so we have to type cast the pointer variable from one type to other type.
int x=27;
void *p;
p=&x;
printf("%d",(((int*)p));
Output :
The statement (int*)p makes p to become an interger type
Example:
#include stdio.h
#include conio.h
main()
{
int x=27;
void*p;
p=&x;
printf("value is=%d",*((int *)p));
}
Output:
Value is 27
Pointer and Functions
1.Pointers can be passed as arguments to a functions definition.
2.Passing pointer to a functions is called call by value.
3.In call by reference what ever changes are made to formal arguments of the functions definition will affect the actual arguments in calling functions.
4.In call by reference the actual arguments must be pointers or references or addresses.
5.Pointer arguments are use full in functions,because they allow accessing the orginal data in the calling program.
Ex1 :
void swap(int*,int*);
main()
{
int a=10,b=20;
swap(&a,&b);
printf("%d,%d",a,b);
}
void swap(int*x,int*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Output: 20 10