Thread: dynamic array

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    dynamic array

    In my code, I am allocating memory for 2 elements first, then assigning value at each memory location. when I print them I get their values.

    then I allocate memoary for more 3 elements then assigning value at each memory location. when I print them I get their values.

    Now I have total five elements which are stoared at five memory location.

    I want to print all the five elements but when i print their value I don't get expected value

    Code:
    #include <stdio.h>
    
     #include <stdlib.h> 
      
    int main() 
    {     
        int i;
      
        // Dynamically allocate memory using malloc() 
          int* ptr = (int*)malloc(2 * sizeof(int)); 
      
        // Check if the memory has been successfully 
        // allocated by malloc or not 
        if (ptr != NULL) 
        { 
            // Memory has been successfully allocated 
            printf("Memory successfully allocated using malloc.\n"); 
      
        // Get the elements of the array 
           for (i = 0; i < 2; i++) 
           { 
               printf("The elements of the array are: ");
               scanf ("%d", ptr + i);            
            } 
      
            // Print the elements of the array 
            printf("The elements of the array are: "); 
            for (i = 0; i < 2; i++) 
            { 
                printf("%d, ", *(ptr  + i)); 
            } 
        } 
     
     
            
        // Dynamically allocate memory using malloc() 
          ptr = (int*)realloc(ptr, 3 * sizeof(int)); 
      
        // Check if the memory has been successfully 
        // allocated by malloc or not 
        if (ptr != NULL)
            { 
              printf("\n Enter the 3 more the elements of the array are \n");
        
               // Get the elements of the array 
               for (i = 0; i < 3; i++) 
                { 
                    printf("The elements of the array are: ");
                    scanf ("%d", ptr + i);            
                } 
      
                // Print the elements of the array 
                printf("\nThe elements of the array are: "); 
                for (i = 0; i < 3; i++) 
                { 
                    printf("%d, ", *(ptr  + i)); 
                } 
            }
            
           // Print the elements of the array 
        printf("\nThe elements of the array are: "); 
        for (i = 0; i < 5; i++) 
        { 
          printf("%d, ", *(ptr  + i)); 
         }     
                 
            
        return 0; 
    }
    Memory successfully allocated using malloc.
    The elements of the array are: 1
    The elements of the array are: 2
    The elements of the array are: 1, 2,
    Enter the 3 more the elements of the array are
    The elements of the array are: 3
    The elements of the array are: 4
    The elements of the array are: 5


    The elements of the array are: 3, 4, 5,
    The elements of the array are: 3, 4, 5, 0, 0,

    Why I don't get output 1, 2, 3, 4, 5?

    Edit : int* ptr = (int*)malloc(5 * sizeof(int));
    Last edited by Dadu@; 02-24-2022 at 11:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  2. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  3. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  4. dynamic array
    By Antimodes in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2009, 06:06 AM
  5. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 10:10 PM

Tags for this Thread