Pointers • A pointer is defined as a derived data type that can store the address of other C variables or a memory location. We can access and manipulate the data stored in that memory location using pointers. • It points to a memory location where the first byte is stored. 40 1002 i Name of the variable Value of the variable Address of the variable 1000 1001 1002 1003 1004 1005 Address Contents Memory
Declaring a pointer Variable Pointer variable contains addresses that belong to a separate data type, they must be declared as pointers before we use them. The syntax for declaring a pointer is data_type * pointer_name; For example: int * ptr; char * ptr; float * ptr; This refers to the type of the value that the pointer will point to
Initialization of pointer variable • Pointer initialization is the process of assigning the address of a variable to pointer variable. • Pointer variable contains address of variable of same data type • The address operator ‘&’ is used to determine the address of a variable. Int x = 5; Int * ptr; Ptr = &x; Address of 5 x 1000 ptr 2000 1000
Code Example Output:
Chain of Pointers • Pointer can point to another pointer. Thus a chain of pointers can be created. • Here the pointer variable P2 contains the address of the pointer variable P1, which points to the location that contains the desired value. This is known as multiple indirections • The variable which is a pointer to a pointer must be declared using additional indirection operator symbol ‘*’. For example: int **P2 Address 2 P2 Address 1 P1 Value Variable
Code Example:
Pointers and Arrays • When an Array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations. • The base address is the location of the first element (index 0) of the array. • The compiler also defines the array name as a constant pointer to the first element.
Properties of array that make it resemble pointers • Array name gives the address of the first element. • Array members are accessed using pointer arithmetic. • When an array is passed, as a parameter to a function, the array name is converted to a pointer to its first element and the function receives the pointer that points to the first element of the array instead of the entire array.
1 2 3 4 5 X[0] X[1] X[2] X[3] X[4] 1000 1004 1008 1012 1016 Elements Address Value Base Address
Pointer Arithmetic • Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. • The pointer variables store the memory address of another variable. It doesn’t store any value. • Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer arithmetic operations are slightly different from the ones that we generally use for mathematical calculations. 1. Increment/Decrement of a Pointer 2. Addition of integer to a pointer 3. Subtraction of integer to a pointer 4. Subtracting two pointers of the same type 5. Comparison of pointers
1. Increment/Decrement of a Pointer • When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. • If an integer pointer that stores address 1000 is incremented, then it will increment by 4(size of an int), and the new address will point to 1004. • When a pointer is decremented, it actually decrements by the number equal to the size of the data type for which it is a pointer. • If an integer pointer that stores address 1000 is decremented, then it will decrement by 4(size of an int), and the new address will point to 996.
2. Addition of Integer to Pointer • When a pointer is added with an integer value, the value is first multiplied by the size of the data type and then added to the pointer. • For Example: If ptr is an integer pointer that stores 1000 as an address. If we add integer 5 to it using the expression, ptr = ptr + 5, then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) x 5 = 1020.
3. Subtraction of Integer to Pointer • When a pointer is subtracted with an integer value, the value is first multiplied by the size of the data type and then subtracted from the pointer similar to addition. • For Example: If ptr is an integer pointer that stores 1000 as an address. If we subtract integer 5 from it using the expression, ptr = ptr – 5, then, the final address stored in the ptr will be ptr = 1000 – sizeof(int) x 5 = 980.
4. Subtraction of Two Pointers • The subtraction of two pointers is possible only when they have the same data type. • The result is generated by calculating the difference between the addresses of the two pointers and calculating how many bits of data it is according to the pointer data type. • The subtraction of two pointers gives the increments between the two pointers. For Example: Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are subtracted. The difference between addresses is 4 bytes. Since the size of int is 4 bytes, therefore the increment between ptr1 and ptr2 is given by (4/4) = 1. This means there is an increment of 1 integer-sized element between ptr1 and ptr2.
5. Comparison of Pointers • We can compare the two pointers by using the comparison operators in C. • We can implement this by using all operators >, >=, <, <=, ==, !=. • It returns true for the valid condition and returns false for the unsatisfied condition.
Types of Pointers in C • Pointers in C can be classified into many different types based on the parameter on which we are defining their types. • If we consider the type of variable stored in the memory location pointed by the pointer, then the pointers can be classified into the following types: 1. Integer Pointers 2. Array Pointers 3. Structure Pointers 4. Function Pointers 5. Double Pointers 6. Null Pointers 7. Void Pointers 8. Wild Pointers 9. Constant Pointers 10. Pointer to Constant 11. Dangling Pointers
Integer Pointers • As the name suggests, these are the pointers that point to the integer values. • These pointers are pronounced as Pointer to Integer. Syntex: int * ptr; • Similarly, a pointer can point to any primary data type. It can also point to derived data types such as arrays and user-defined data types such as structures.
Array Pointers • The array name is the pointer to its first element. • They are also known as Pointer to Arrays. We can create a pointer to an array using the given syntax. Syntax: char *ptr = &array_name;
Structure Pointer • The pointer pointing to the structure type is called Structure Pointer or Pointer to Structure. Syntax: struct struct_name *ptr;
Function Pointer • Function pointers point to the functions. • They are different from the rest of the pointers in the sense that instead of pointing to the data, they point to the code. • Let’s consider a function prototype – int func (int, char), the function pointer for this function will be Syntax: int (*ptr)(int, char); *ptr is a pointer that points to a function that returns an integer value and accepts integer and character as arguments Return type Pointer name Arguments of the function
Code Example:
Code Example:
Passing a function's address as an argument to other function • We can pass the function's address as an argument to other functions in the same way we send other arguments to the function.
Double Pointers • A pointer that stores the memory address of another pointer. • Such pointers are called double-pointers or pointers-to-pointer. • Instead of pointing to a data value, they point to another pointer. Syntax: datatype ** pointer_name; • Dereferencing Double Pointer *pointer_name; // get the address stored in the inner level pointer **pointer_name; // get the value pointed by inner level pointer Note: In C, we can create multi-level pointers with any number of levels such as : ***ptr3, ****ptr4, ******ptr5 and so on.
Null Pointers • The Null Pointers are those pointers that do not point to any memory location. • They can be created by assigning a NULL value to the pointer. A pointer of any type can be assigned the NULL value. Syntax: data_type *pointer_name = NULL; or pointer_name = NULL
Void Pointers • The Void pointers in C are the pointers of type void. They do not have any associated data type. • They are also called generic pointers as they can point to any type and can be typecasted to any type. Syntax: void * pointer_name; One of the main properties of void pointers is that they cannot be dereferenced.
Code Example:
Wild Pointers • The Wild Pointers are pointers that have not been initialized with something yet. • These types of C-pointers can cause problems in our programs and can eventually cause them to crash. • If values is updated using wild pointers, they could cause data abort or data corruption. Example: int *ptr; char *str;
Constant Pointers • In constant pointers, the memory address stored inside the pointer is constant and cannot be modified once it is defined. • It will always point to the same memory address. Syntax: data_type * const pointer_name;
Code Example
Pointer to Constant • The pointers pointing to a constant value that cannot be modified are called pointers to a constant. • We can only access the data pointed by the pointer, but cannot modify it. Although, we can change the address stored in the pointer to constant. Syntax: const data_type * pointer_name;
Code Example:
Dangling Pointers • A dangling pointer in C is a pointer that points to a memory location that has been deallocated or is no longer valid. • Dangling pointers can cause various problems in a program, including segmentation faults, memory leaks, and unpredictable behavior. • One common cause of dangling pointers is using the free function to deallocate memory that was previously allocated using the malloc function. • When the free function is called on a pointer, it deallocates the memory pointed to by the pointer, making it available for reuse. • However, if the pointer is not set to NULL or reassigned to a different memory location after the memory is deallocated, it becomes a dangling pointer.
Introduction to pointers in C, types of pointers

Introduction to pointers in C, types of pointers

  • 1.
    Pointers • A pointeris defined as a derived data type that can store the address of other C variables or a memory location. We can access and manipulate the data stored in that memory location using pointers. • It points to a memory location where the first byte is stored. 40 1002 i Name of the variable Value of the variable Address of the variable 1000 1001 1002 1003 1004 1005 Address Contents Memory
  • 2.
    Declaring a pointerVariable Pointer variable contains addresses that belong to a separate data type, they must be declared as pointers before we use them. The syntax for declaring a pointer is data_type * pointer_name; For example: int * ptr; char * ptr; float * ptr; This refers to the type of the value that the pointer will point to
  • 3.
    Initialization of pointervariable • Pointer initialization is the process of assigning the address of a variable to pointer variable. • Pointer variable contains address of variable of same data type • The address operator ‘&’ is used to determine the address of a variable. Int x = 5; Int * ptr; Ptr = &x; Address of 5 x 1000 ptr 2000 1000
  • 4.
  • 5.
    Chain of Pointers •Pointer can point to another pointer. Thus a chain of pointers can be created. • Here the pointer variable P2 contains the address of the pointer variable P1, which points to the location that contains the desired value. This is known as multiple indirections • The variable which is a pointer to a pointer must be declared using additional indirection operator symbol ‘*’. For example: int **P2 Address 2 P2 Address 1 P1 Value Variable
  • 6.
  • 7.
    Pointers and Arrays •When an Array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations. • The base address is the location of the first element (index 0) of the array. • The compiler also defines the array name as a constant pointer to the first element.
  • 8.
    Properties of arraythat make it resemble pointers • Array name gives the address of the first element. • Array members are accessed using pointer arithmetic. • When an array is passed, as a parameter to a function, the array name is converted to a pointer to its first element and the function receives the pointer that points to the first element of the array instead of the entire array.
  • 9.
    1 2 34 5 X[0] X[1] X[2] X[3] X[4] 1000 1004 1008 1012 1016 Elements Address Value Base Address
  • 10.
    Pointer Arithmetic • PointerArithmetic is the set of valid arithmetic operations that can be performed on pointers. • The pointer variables store the memory address of another variable. It doesn’t store any value. • Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer arithmetic operations are slightly different from the ones that we generally use for mathematical calculations. 1. Increment/Decrement of a Pointer 2. Addition of integer to a pointer 3. Subtraction of integer to a pointer 4. Subtracting two pointers of the same type 5. Comparison of pointers
  • 11.
    1. Increment/Decrement ofa Pointer • When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. • If an integer pointer that stores address 1000 is incremented, then it will increment by 4(size of an int), and the new address will point to 1004. • When a pointer is decremented, it actually decrements by the number equal to the size of the data type for which it is a pointer. • If an integer pointer that stores address 1000 is decremented, then it will decrement by 4(size of an int), and the new address will point to 996.
  • 13.
    2. Addition ofInteger to Pointer • When a pointer is added with an integer value, the value is first multiplied by the size of the data type and then added to the pointer. • For Example: If ptr is an integer pointer that stores 1000 as an address. If we add integer 5 to it using the expression, ptr = ptr + 5, then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) x 5 = 1020.
  • 15.
    3. Subtraction ofInteger to Pointer • When a pointer is subtracted with an integer value, the value is first multiplied by the size of the data type and then subtracted from the pointer similar to addition. • For Example: If ptr is an integer pointer that stores 1000 as an address. If we subtract integer 5 from it using the expression, ptr = ptr – 5, then, the final address stored in the ptr will be ptr = 1000 – sizeof(int) x 5 = 980.
  • 16.
    4. Subtraction ofTwo Pointers • The subtraction of two pointers is possible only when they have the same data type. • The result is generated by calculating the difference between the addresses of the two pointers and calculating how many bits of data it is according to the pointer data type. • The subtraction of two pointers gives the increments between the two pointers. For Example: Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are subtracted. The difference between addresses is 4 bytes. Since the size of int is 4 bytes, therefore the increment between ptr1 and ptr2 is given by (4/4) = 1. This means there is an increment of 1 integer-sized element between ptr1 and ptr2.
  • 18.
    5. Comparison ofPointers • We can compare the two pointers by using the comparison operators in C. • We can implement this by using all operators >, >=, <, <=, ==, !=. • It returns true for the valid condition and returns false for the unsatisfied condition.
  • 20.
    Types of Pointersin C • Pointers in C can be classified into many different types based on the parameter on which we are defining their types. • If we consider the type of variable stored in the memory location pointed by the pointer, then the pointers can be classified into the following types: 1. Integer Pointers 2. Array Pointers 3. Structure Pointers 4. Function Pointers 5. Double Pointers 6. Null Pointers 7. Void Pointers 8. Wild Pointers 9. Constant Pointers 10. Pointer to Constant 11. Dangling Pointers
  • 21.
    Integer Pointers • Asthe name suggests, these are the pointers that point to the integer values. • These pointers are pronounced as Pointer to Integer. Syntex: int * ptr; • Similarly, a pointer can point to any primary data type. It can also point to derived data types such as arrays and user-defined data types such as structures.
  • 22.
    Array Pointers • Thearray name is the pointer to its first element. • They are also known as Pointer to Arrays. We can create a pointer to an array using the given syntax. Syntax: char *ptr = &array_name;
  • 23.
    Structure Pointer • Thepointer pointing to the structure type is called Structure Pointer or Pointer to Structure. Syntax: struct struct_name *ptr;
  • 24.
    Function Pointer • Functionpointers point to the functions. • They are different from the rest of the pointers in the sense that instead of pointing to the data, they point to the code. • Let’s consider a function prototype – int func (int, char), the function pointer for this function will be Syntax: int (*ptr)(int, char); *ptr is a pointer that points to a function that returns an integer value and accepts integer and character as arguments Return type Pointer name Arguments of the function
  • 25.
  • 26.
  • 27.
    Passing a function'saddress as an argument to other function • We can pass the function's address as an argument to other functions in the same way we send other arguments to the function.
  • 28.
    Double Pointers • Apointer that stores the memory address of another pointer. • Such pointers are called double-pointers or pointers-to-pointer. • Instead of pointing to a data value, they point to another pointer. Syntax: datatype ** pointer_name; • Dereferencing Double Pointer *pointer_name; // get the address stored in the inner level pointer **pointer_name; // get the value pointed by inner level pointer Note: In C, we can create multi-level pointers with any number of levels such as : ***ptr3, ****ptr4, ******ptr5 and so on.
  • 29.
    Null Pointers • TheNull Pointers are those pointers that do not point to any memory location. • They can be created by assigning a NULL value to the pointer. A pointer of any type can be assigned the NULL value. Syntax: data_type *pointer_name = NULL; or pointer_name = NULL
  • 30.
    Void Pointers • TheVoid pointers in C are the pointers of type void. They do not have any associated data type. • They are also called generic pointers as they can point to any type and can be typecasted to any type. Syntax: void * pointer_name; One of the main properties of void pointers is that they cannot be dereferenced.
  • 31.
  • 32.
    Wild Pointers • TheWild Pointers are pointers that have not been initialized with something yet. • These types of C-pointers can cause problems in our programs and can eventually cause them to crash. • If values is updated using wild pointers, they could cause data abort or data corruption. Example: int *ptr; char *str;
  • 33.
    Constant Pointers • Inconstant pointers, the memory address stored inside the pointer is constant and cannot be modified once it is defined. • It will always point to the same memory address. Syntax: data_type * const pointer_name;
  • 34.
  • 35.
    Pointer to Constant •The pointers pointing to a constant value that cannot be modified are called pointers to a constant. • We can only access the data pointed by the pointer, but cannot modify it. Although, we can change the address stored in the pointer to constant. Syntax: const data_type * pointer_name;
  • 36.
  • 37.
    Dangling Pointers • Adangling pointer in C is a pointer that points to a memory location that has been deallocated or is no longer valid. • Dangling pointers can cause various problems in a program, including segmentation faults, memory leaks, and unpredictable behavior. • One common cause of dangling pointers is using the free function to deallocate memory that was previously allocated using the malloc function. • When the free function is called on a pointer, it deallocates the memory pointed to by the pointer, making it available for reuse. • However, if the pointer is not set to NULL or reassigned to a different memory location after the memory is deallocated, it becomes a dangling pointer.