2. Introduction to Pointers
A pointer is a variable that holds a memory address. This address is the location of
another object(typically another variable) in a memory.
3. Pointers notations in C
You can use a pointer on the right-hand side of an assignment statement to
assign its value to another pointer. When both Pointers are the same type, the
situation is straightforward.
4. Declaration of pointer
A pointer is declared as “data-type*pointer-variable-name”.
* symbol specifies it is a pointer variable. You must prefix * before variable name
to declare it as a pointer.
Pointer-variable-name is a valid C identifier i.e. the name of pointer variable.
5. Operations that can be performed on a computer
Assignment
Value finding
Taking a pointer address
Adding an integer to a pointer
Incrementing a pointer
Subtracting an integer from a pointer
Decrementing a pointer
Differencing
comparision
6. Use of pointers in programming exercises
Pointers are more efficient in handling arrays and structures
Pointers are used to return multiple values from a function
Pointer allow dynamic memory allocation and deallocation in C.
Pointers allow to refer and pass a function as a parameter to functions
7. Parameters passing in pointers
These are of two types
Call by value
Call by reference
8. Call by value
In call by value method, the value of actual parameters is copied into the formal
parameters in other words we can say that the value of variable is used in the
function call in the call by value method.
The actual parameters is the argument which is used in the function call whereas
formal parameter is the argument which is used in the function definition.
9. Call by reference
In call by reference, the address of the variable is passed into the function call as
the actual parameters.
The value of the actual parameters can be modified by changing the formal
parameter since the address of the actual parameter is passed.
12. Malloc
It stores data in a one memory chunk
It does not initializes memory at 0
When memory is insufficient it points at NULL value
Syntax: <pointer_name>=(cast type *)malloc(size)
13. Calloc
It creates multiple block of memory
It points at NULL value if memory is insufficient
It initializes memory at 0
Suntax : <pointer_name>=(cast_type *)calloc(n,size)
14. Realloc
It is used to increase or decrease the size of memory. Basically used after malloc or
calloc
Syntax: <pointer_name>=realloc(ptr_name,new size)
15. Free
Put it at the place(usually end of program) to remove the memory of at pointer
Syntax: free(pointer_name)