Skip to main content

Posts

Showing posts from November, 2013

Dangling pointers

What is dangling pointer? If a pointer refers to an invalid memory location, then it is called dangling pointer. Consider any pointer that points to a newly allocated memory block.  If the newly allocated block is freed without modifying the pointer value, then the pointer will point to the deallocated memory block.  So, corruption might occur if the user tries to perform write operation in the memory block pointed by dangling pointer.  In most cases, user can observe segmentation fault when he tries to free/dereference a dangling pointer.       int *ptr , *tmp;       ptr = (int *)malloc(sizeof(int));       tmp = ptr;       free(ptr);       *tmp = *tmp + 10 Here, tmp is a dangling pointer since it points to the freed memory block. Below is an another example for dangling pointer.       int *ptr;      {              int ch = 10; ...

Sort n numbers in ascending order using pointers

C program to sort n numbers in ascending order using pointers   #include <stdio.h>   #include <stdlib.h>   int main() {         int i, j, n, tmp, *ptr;         /* get the number of inputs from the user */         printf("Enter number of inputs:");         scanf("%d", &n);         /* dynamic memory allocation for n elements */         ptr = (int *)malloc(sizeof(int) * n);         /* get the inputs from the user */         printf("Enter your inputs:\n");         for (i = 0; i < n; i++) {                 scanf("%d", ptr + i);         }         /* sort the given numbers in ascending order */         for (i = 0; i < n - 1; i++) {               ...

Pointer to union

Union pointers: What is pointer to union? If the pointer refers to the address of the union variable, then the pointer is called  pointer to union or union pointer.      union val {              int ival;              float fval;              char cval;      } obj, *ptr;      ptr = &obj; Here, pointer ptr refers to the address of the union variable obj . How to access and assign values to union elements using pointers? Dereference operator "->" is used to access and assign values to union using pointers.        ptr->ival = 10;        ptr->fval = 20.22;        ptr->cval = 'a'; Note: Only one data member will be active at a time. The above set of statements is equivalent to         (*ptr).ival = 10;        (*ptr).fva...

Pointers to structures

Structure pointers What is structure pointer? If the pointer refers to the address of the structure variable, then the pointer is called structure pointer or pointer to structure.      struct student { char name[32]; int rollno, marks[5]; float average;      } obj, *ptr;      ptr = &obj; Here, pointer ptr refers to the address of the structure variable obj . C program using structure pointer:   #include <stdio.h>   /* structure st with three different members */   struct st {         char ch;         int num;         float val;   } obj, *ptr; // one structure object and a pointer to structure     int main() {         /* assigning address of structure variable to pointer */         ptr = &obj;         /* assigning values to structure elements ...

Character pointer

Pointer to character: What is character pointer? If the base type of a pointer is a character, then the pointer is called character pointer or pointer to character. How to declare character pointer? Below is the declaration for character pointer.           char *<variable_name>;           char *ptr; How to initialize character pointer in c? Below are few examples for character pointer initialization.      char *ptr = NULL;      char *ptr = "Hello world"; What is the size of character pointer? The size of character pointer can be obtained using the sizeof() operator Program:   #include <stdio.h>   int main() {         char *cptr;         printf("Sizeof character pointer: %d bytes\n", sizeof(cptr));         return 0;   }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Sizeof character pointer: 4 bytes How character...

Pointer to pointer

Pointer is a variable that stores address of another variable.  Whereas, pointer to pointer is nothing but a variable that stores the address of another pointer. Below is the declaration for pointer to pointer       <datatype>  **<variable_name>       char **ptr;  // pointer to pointer to char Let us see how pointer to pointer works.  Consider the below example        char ch = 'a';        char *cptr, **dptr;        cptr = &ch;  // pointer stores address  of other variable        dptr = &cptr;  // pointer to pointer stores address of another pointer dptr     - address of cptr *dptr   - address of the variable ch **dptr - value in ch which is 'a' Pointer to pointer is also called as double pointer.  Let us see how to do memory allocation for double pointer. Dynamic memory allocation for double pointer: ...

Pointer arithmetic - operations with pointers

Operations with pointers: User can perform below operation with pointers: Adding an integer to pointer Subtracting an integer from pointer Subtracting one pointer from another Comparing two pointers Adding an integer to pointer: Consider ptr as a pointer to the array element arr[i] , then ptr++ increments ptr to point to the array element arr[i + 1] .  Similarly, if ptr points to the array element arr[i], and j is an integer, then "ptr = ptr + j" makes the pointer "ptr" to point to the array element arr[i + j].     #include <stdio.h>   int main() {         int *ptr, i = 0, arr[5] = {10, 20, 30, 40, 50};         ptr = &arr[i];         printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, arr[i]);         printf("ptr: 0x%x\t&arr[%d]: 0x%x\n", (int)ptr, i, (int)&arr[i]);         ++ptr, i++;         printf("*ptr: %d\tarr[%d]: %d\t", *ptr, i, a...