Fundamentals of pointers UNIT IV By : Shivanshu Verma
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.
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.
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.
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
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
Parameters passing in pointers  These are of two types  Call by value  Call by reference
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.
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.
Array and character using pointers
Dynamic Allocation  It can be done by four methods 1. Malloc 2. Calloc 3. Realloc 4. free
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)
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)
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)
Free Put it at the place(usually end of program) to remove the memory of at pointer Syntax: free(pointer_name)
Thank You

Fundamentals of Pointers in C

  • 1.
    Fundamentals of pointers UNITIV By : Shivanshu Verma
  • 2.
    Introduction to Pointers Apointer 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 inC  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 canbe 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 pointersin 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 inpointers  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.
  • 10.
    Array and characterusing pointers
  • 11.
    Dynamic Allocation  Itcan be done by four methods 1. Malloc 2. Calloc 3. Realloc 4. free
  • 12.
    Malloc  It storesdata 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 createsmultiple 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 isused 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 atthe place(usually end of program) to remove the memory of at pointer Syntax: free(pointer_name)
  • 16.