Pointers – the most powerful feature of C.
In fact, it is said that without using the pointers,
one can’t use C efficiently & effectively in a real world program!
Pointers save memory space. Execution time with pointers is faster
because data are manipulated with the address, that is,
direct access to memory location. Memory is accessed efficiently with the pointers.
The pointer assigns and releases the memory as well.
Each variable is assigned a memory locations (the size depends on the data type)
and the variable’s data is stored there.
Name of the variable is a reference to that memory address.
int num = 0x150; //all addresses are names in HEX format
num
➢A pointer is a variable used to store the address of a memory cell.
➢We can use the pointer to reference the memory cell
➢Pointers are the Variables that contain memory addresses as
their values.
➢Pointers to int type
➢Pointers to char type
➢Pointes to float type
➢Pointers to Structure/Union
➢Pointers to Functions
➢& operator returns the “address of the variable” in memory.
ex:-
int *p;
int num;
&num -> address of the no
&p -> address of the pointer
--------------------------------
➢NULL – Built in constant,that has the value 0.
To initialize a pointer variable when that pointer variable isn’t
assigned any valid memory address yet
➢Use the address(&) operator to get address of a variable
and assign it to the pointer.
In many programming languages, including C and C++, a null pointer is a special
value that is used to indicate that a pointer does not point to a valid location
in memory. A null pointer is often used as a sentinel value to indicate the end of
a list or to indicate that a pointer is not pointing to any valid object. It is
also sometimes used to initialize a pointer to a known invalid value, as a way to
ensure that the pointer has been initialized before it is used. In general, null
pointers are used as a safety measure to prevent the program from dereferencing
an uninitialized or invalid pointer, which can lead to undefined behavior and
potentially serious errors such as segmentation faults.
ex:-
int a;
int *p = &a;
int *p = NULL;
--------------------------------
Indirection(*)Operator
➢Pointer variable contains memory address of the variable.
➢To refer or access the contents of the variable that the pointer points to,
we use indirection operator.
Example:-
int *p;
int num;
p=#
printf("%d",*p)// value will be reflected
--------------------------------
Dangling Pointer
A pointer pointing to a memory location that has been deleted(or freed) is called dangling pointer.
Exmaple:-
intmain()
{
int*ptr= (int*)malloc(sizeof(int));//after the free,ptr becomes dangling pointer
free(ptr);
}
voidmain()
{
int*ptr; // dangling occurs
......
}
--------------------------------
void pointer
A void pointer is nothing but a pointer variable declared using there served word
in C'void’.
➢When a pointer variable is declared using keyword
void–it becomes a general purpose pointer variable.
➢Address of any variable of any datatype(char,int,floatetc.)
can be assigned to avoid pointer variable.
void*ptr;
Example:-
int a;
void*ptr;
ptr=&a;
printf(“The value of the integer variable is %d:”,*(int*))
--------------------------------
To embed this program on your website, copy the following code and paste it into your website's HTML: