[go: up one dir, main page]

0% found this document useful (0 votes)
18 views6 pages

Pointer C Notes

Pointer is a variable that stores the address of another variable. Pointers allow manipulation of data using memory addresses. The main purpose of pointers is dynamic memory allocation. There are different types of pointers including NULL pointers, void pointers, and dangling pointers. Dynamic memory allocation using functions like malloc(), calloc(), and realloc() allow programs to allocate memory at runtime. Pointers offer benefits like saving memory space and faster execution times since data can be directly accessed via memory addresses.

Uploaded by

Maha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Pointer C Notes

Pointer is a variable that stores the address of another variable. Pointers allow manipulation of data using memory addresses. The main purpose of pointers is dynamic memory allocation. There are different types of pointers including NULL pointers, void pointers, and dangling pointers. Dynamic memory allocation using functions like malloc(), calloc(), and realloc() allow programs to allocate memory at runtime. Pointers offer benefits like saving memory space and faster execution times since data can be directly accessed via memory addresses.

Uploaded by

Maha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Pointer :

Pointer is a variable which stores address of another variable.


The important point about pointer is they allow us t manipulate the data
using memory.
The main goal of pointer is : Dynamic Memory Allocation.
eg :
int a = 10 ; //normal variable.
int *ptr; //Pointer variable with astricks operator.
ptr = &a; //in pointer ptr we have store the address of variable a
print("Value of A = %d", *ptr);

Note : Using dereferencing operator we can access value of a variable


which address is stored on pointer variable.

Uses Of Pointer:
1> Used to access the array.
2> Used in Dynamic memory allocation.
3> Used to pass arguments by the reference.

IMPORTANT :
1> Pointer always use address of another variable.
2> Pointer has its own address.
3> Pointer can access the value of the variable, whose address is
stored in pointer.
(using dereference * operator).
4> If we increase the ptr without * then pointer get increase by memory block.
means if int ptr is pointing to 1000, if we increase pointer like : ptr++ then it
will point to 1004.

EXAMPLE :
void main()
{
int a= 100 , *ptr;
ptr = &a;

*ptr = 200;
printf("A = %d\t", a);
}

Output :
200
as we have modify the value of a using pointer variable.
___________________________________________________________________________________
Types of Pointer :
A Pointer is used to store the "memory address" of other variable.
1> NULL Pointer :
-pointer that do not point to any memory location.
-they are initialized with NULL value.

Uses :
> It is used when you dont want to pass any valid memory address.
> used in data structure like trees, linked list to indicates the end.

syntax : type pointer_name = NULL;

2> Void Pointer :


-Void pointer is also called as "generic pointer".
-void pointers of type void it means that they do not have any associated
data type.
-but this pointer can be typecasted to any data type hence called - generic
Pointer.
-we can not dereference the void pointer.
-It is capable of storing addresses of any data type.

Uses :
this pointer is used in dynamic memory allocation.
As we can typecast the pointer with any type.
so this pointer is used with calloc(), malloc() and realloc().

syntax : pointer_name = (void*)<data_type>;

3> Dangling Pointer :


-The pointer pointing to a deallocated memory block are known as -
"Dangling Pointer".
-Dangling pointer is occurs at the time of the object destruction , when
object is deleted without modifying
value of the pointer.

eg:
int main()
{
int *ptr; //pointer
ptr = (int*)malloc(sizeof(int)); //we have assign the memory
//for single variable.
int a = 17;
ptr = &a; // we have assigned memory address of a int
pointer.

free(ptr); //deallocates the memory


}

Note : Now pointer ptr is pointing to deallocated memory block.


To avoid this situation : assign pointer variable to NULL.

eg = ptr = NULL;

4> Wild Pointer :


-A pointer which is not initialized to anything (not even NULL) is known as
Wild Pointer.
-Pointer may be initialize to non-NULL garbage value that may be not valid
address.
-As known as - Bad Pointer.
eg:
int main()
{
int *ptr; //wild pointer.
int x=10;

ptr = &x; //now it is not wild or bad pointer.


}

___________________________________________________________________________________
Difference Between : Static Memory Allocation and Static Memory Allocation :

What is Dynamic Memory Allocation ?

It enables the Programmer to allocate memory at runtime.


Dynamic memory allocation possible by following :

1. malloc():
It is available in header file : stdlib.h
It is refers as the “Memory Allocation” .
This function allocates the single block of requested memory with
default value :
Garbage value.
It is faster than the calloc()

Syntax : ptr = (cast_type*)malloc(sizeof(data_type) * requested_size);

2. calloc():
It is refers as the “Contiguous Memory Allocation”.
This function allocates the separate block for each variable with
default value : 0.
It is slower than malloc().

Syntax : ptr = (cast_type*) calloc(sizeof(data_type), requested_size);

3. realloc():
It I refers as “Reallocation of a Memory”.
If Memory allocated by the malloc() and calloc() is not sufficient then
user can
Reallocates the memory using realloc() function.
In short it is used to expand the memory.

Syntax : ptr = realloc(ptr, new-size);

Difference : Malloc() and Calloc() :

Application of Pointer :
1. Pointers save memory space.
2. Execution time with pointers is faster because data is directly manipulated with
the address as
Pointer direct access to memory location.
3. Pointers are used with array , string and function.
4. Pointers are used in dynamic memory allocation and file handling to.

You might also like