Thread: Value is not being stored at correct location

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    Value is not being stored at correct location

    I am trying to allocate dynamic memory for five integer to store value from 0 to 4
    as shown

    00DE2AB4 = 0
    00DE2AB8 = 1
    00DE2ABC = 2
    00DE2AC0 = 3
    00DE2AC4 = 4

    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    int main ()
    {
      int *x = malloc(5 * sizeof(x));
      
      int i = 0;
      for ( i = 0; i < 5; i++)
      {
          x++;
          printf( "Value of x[%d] = %p\n", i, x );
              
      }
      
      printf("\n");
      
      for ( i = 0; i < 5; i++)
      {
          *x = 1;
          *x++;
          printf("Value %d store at address %p\n", *x ,  i);
      }
      
      return 0;
      
    }
    Value of x[0] = 00022AB4
    Value of x[1] = 00022AB8
    Value of x[2] = 00022ABC
    Value of x[3] = 00022AC0
    Value of x[4] = 00022AC4


    Value 902931021 store at address 00000000
    Value 134222834 store at address 00000001
    Value 1970037078 store at address 00000002
    Value 959520869 store at address 00000003
    Value 808596793 store at address 00000004



    Program output show that memory has been allocated for five integer but value are not being stored at address. Can anyone point out what is reason for this ?
    Last edited by Rahul11; 09-19-2021 at 11:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-02-2016, 08:47 PM
  2. Replies: 3
    Last Post: 05-13-2015, 06:45 AM
  3. Defining the correct location of a type declaration
    By vinians in forum C Programming
    Replies: 3
    Last Post: 03-22-2015, 07:42 PM
  4. How objects are stored
    By MTK in forum C++ Programming
    Replies: 42
    Last Post: 12-01-2009, 05:29 PM
  5. decimal being stored as 0
    By scoketboy in forum C Programming
    Replies: 7
    Last Post: 11-05-2005, 02:31 AM

Tags for this Thread