I think a[j] is being calculated by computer by assuming all rows are stored in a continuous manner , with no gaps between them .Ofcourse , individual elements in each row are also stored sequentially .
then why this code works .. here rows are not stored directly after one other ..
#include<iostream>using namespace std;int main(){ int rows = 3; int cols = 4; int** a = (int**)malloc(rows*sizeof(int*)); for(int i =0 ;i<rows;i++){ a = (int*)malloc(cols*sizeof(int));}for(int i = 0 ; i<rows;i++){ cout<<"\n"; for( int j = 0 ;j < cols;j++) { cout<<&a[j]<<" "; }}for(int i = 0 ; i<rows;i++){ cout<<"\n"; for( int j = 0 ;j < cols;j++) { a[j] = i+j; }}cout<<"\n********************88\n";for(int i = 0 ; i<rows;i++){ cout<<"\n"; for( int j = 0 ;j < cols;j++) { cout<<a[j]<<" "; }}}
output is
0x8051468 0x805146c 0x8051470 0x8051474
0x8051498 0x805149c 0x80514a0 0x80514a4
0x80514c8 0x80514cc 0x80514d0 0x80514d4
********************88
0 1 2 3
1 2 3 4
2 3 4 5
please someone explain >>
[Edited by - Zahlman on October 16, 2010 4:42:59 PM]