Professional Documents
Culture Documents
Pointer
Pointer
DEFINITION:
In computer science, a pointer is a programming language
data type whose value refers directly to (or "points to")
another value stored elsewhere in the computer memory
using its address
It contains only the Memory Location of the variable
rather than its content.
int X = 547;
int *ptr;
ptr=&X;
HERE:
Variable
name
ptr
Content
s
Locatio
n
547
4000
4000
4036
Advantages of Pointers
To point to different data structures
To achieve clarity and simplicity
More compact and efficient coding
To return multiple values via functions
Dynamic memory Allocation
POINTER DECLARATION
In c Every Variable must be Declared its type
,since pointer variables contain address of separate
Data type , They must be Declared before use them
the declaration of pointer variable takes the
following form
Syntax:
data _type
name
* pt_
EXAMPLE:
1. Int *p ;
2. float *p;
HERE:
1. Declares the variable p as a pointer variable that points to an integer data
type
2. Declares the variable p as a pointer variable that points to an float data type
POINTER
INITIALIZATION
As ordinary variables are Initialized with in Declaration
part of the program ,
The Pointer variables can initialized by Accessing address of the
variable that are
Used in the program .
Syntax
:
Data_type *ptr =
expression
Where:
data_type = Any Basic Data type
*ptr
= pointer variable
PROGRAM EXAMPLE
# include < stdio.h>
# include < conio.h>
void main()
{
int a=486;
int *b=NULL ;
b=&a;
printf( Address of a = %d ,b);
printf ( The content of a = %d ,*b);
}
OUT PUT:
Address of a = 4056
The content of a = 486486
CONCLUSION
Pointers are one of the most useful
and dangerous features in c.
Unintialised pointers can cause the
system to crash and moreover they
can create bugs within the
programs.
But their advantages overshadow
their limitations