Thread: memory allocation for two dimensional array

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    66

    memory allocation for two dimensional array

    I have declared two dimensional array. I want to print array element and address of array element

    I wrote program here
    Code:
    #include<stdio.h>
    
    void main()
    {
        int i, j;
        int array[2][4]={1, 2, 3, 4, 5, 6, 7, 8};
         for ( i=0; i<2; i++)
              for ( j=0; j<4; j++)
                  printf("%d\n", array[i][j]);
                  printf("%p\n", &array[i][j]);
    }
    1
    2
    3
    4
    5
    6
    7
    8
    0061FF38

    why program is not showing all the memory allocation ?

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,762
    Because you don't have opening and closing curly braces where you need them

    Your code is this
    Code:
    #include<stdio.h>
    
    void main()
    {
        int i, j;
        int array[2][4]={1, 2, 3, 4, 5, 6, 7, 8};
         for ( i=0; i<2; i++)
              for ( j=0; j<4; j++)
                  printf("%d\n", array[i][j]);
         printf("%p\n", &array[i][j]);
    }
    Unrelated, but main returns int, not void

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    66
    Quote Originally Posted by Hodor View Post
    Because you don't have opening and closing curly braces where you need them
    Thanks
    Code:
    #include<stdio.h> 
    int main()
    {
        int i, j;
        int array[2][4]={1, 2, 3, 4, 5, 6, 7, 8};
         for ( i=0; i<2; i++) 
         for ( j=0; j<4; j++)
    	 {
            printf("%d\n", array[i][j]);		
            printf("%p\n", &array[i][j]);
    	 }
    	 
    	 return 0;
    }
    1
    0061FF08
    2
    0061FF0C
    3
    0061FF10
    4
    0061FF14
    5
    0061FF18
    6
    0061FF1C
    7
    0061FF20
    8
    0061FF24

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is possible access memory like 2-3 dimensional array?
    By Vanhapolle in forum C Programming
    Replies: 4
    Last Post: 12-28-2014, 08:54 AM
  2. 3-dimensional array dynamic allocation
    By alexiy in forum C Programming
    Replies: 7
    Last Post: 05-03-2010, 10:48 PM
  3. Replies: 3
    Last Post: 03-03-2010, 03:35 PM
  4. 4-dimensional array contiguous allocation
    By ShaunMahony in forum C Programming
    Replies: 24
    Last Post: 05-23-2005, 02:09 PM
  5. memory allocation problem with 2 dimensional array
    By nano_nasa in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 11:34 AM

Tags for this Thread