fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using std::setw;
  4. using std::cout;
  5. int main() {
  6. int width = 2;
  7. int height = 2;
  8. int size = height * sizeof(int *) + width * height * sizeof(int);
  9. char *array = new char [33];
  10. int i = 0;
  11. for(char tmp = 'A'; tmp < 'A'+32; ++tmp) {
  12. array[i++] = tmp;
  13. }
  14. array[i] = 0;
  15.  
  16. unsigned char *c = (unsigned char*)array;
  17. int **data = (int**)array;
  18. cout << std::hex << std::left ;
  19. for(i = 0; i < 16; ++i) {
  20. cout << "&c[" << setw(2) << i << "] = " << (void*)(c+i)
  21. << ", c[" << setw(2) << i << "] = 0x" << setw(12) << (unsigned)(c[i])
  22. << "&c[" << setw(2) << i+16 << "] = " << (void*)(c+i+16)
  23. << ", c[" << setw(2) << i+16 << "] = 0x" << (unsigned)(c[i+16]) << '\n';
  24. }
  25.  
  26. for(i = 0; i < height; i++) {
  27. data[i] = (int*)data + height + i * width;
  28. cout << "&data[" << i << "] = " << (int*)(&data[i]) << '\t';
  29. cout << "data[" << i << "] = " << data[i] << '\t';
  30. cout << "data[" << i << "] should be " << (int*)((char*)(data + height) + i * width * sizeof(int)) << '\n';
  31. }
  32.  
  33. int v = 1;
  34. for(i = 0; i < height; ++i) {
  35. for(int j = 0; j < width; ++j) {
  36. cout << "&data[" << i << "][" << j << "] = " << (int*)&(data[i][j]) << '\t';
  37. // data[i][j] = v++; <- BEWARE! this line will cause segmentation fault!
  38. cout << "data[" << i << "][" << j << "] = " << data[i][j] << '\n';
  39. }
  40. }
  41.  
  42. for(i = 0; i < 16; ++i) {
  43. cout << "&c[" << setw(2) << i << "] = " << (void*)(c+i)
  44. << ", c[" << setw(2) << i << "] = 0x" << setw(12) << (unsigned)(c[i])
  45. << "&c[" << setw(2) << i+16 << "] = " << (void*)(c+i+16)
  46. << ", c[" << setw(2) << i+16 << "] = 0x" << (unsigned)(c[i+16]) << '\n';
  47. }
  48.  
  49. delete [] array;
  50. }
  51.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
&c[0 ] = 0x9927008, c[0 ] = 0x41          &c[10] = 0x9927018, c[10] = 0x51
&c[1 ] = 0x9927009, c[1 ] = 0x42          &c[11] = 0x9927019, c[11] = 0x52
&c[2 ] = 0x992700a, c[2 ] = 0x43          &c[12] = 0x992701a, c[12] = 0x53
&c[3 ] = 0x992700b, c[3 ] = 0x44          &c[13] = 0x992701b, c[13] = 0x54
&c[4 ] = 0x992700c, c[4 ] = 0x45          &c[14] = 0x992701c, c[14] = 0x55
&c[5 ] = 0x992700d, c[5 ] = 0x46          &c[15] = 0x992701d, c[15] = 0x56
&c[6 ] = 0x992700e, c[6 ] = 0x47          &c[16] = 0x992701e, c[16] = 0x57
&c[7 ] = 0x992700f, c[7 ] = 0x48          &c[17] = 0x992701f, c[17] = 0x58
&c[8 ] = 0x9927010, c[8 ] = 0x49          &c[18] = 0x9927020, c[18] = 0x59
&c[9 ] = 0x9927011, c[9 ] = 0x4a          &c[19] = 0x9927021, c[19] = 0x5a
&c[a ] = 0x9927012, c[a ] = 0x4b          &c[1a] = 0x9927022, c[1a] = 0x5b
&c[b ] = 0x9927013, c[b ] = 0x4c          &c[1b] = 0x9927023, c[1b] = 0x5c
&c[c ] = 0x9927014, c[c ] = 0x4d          &c[1c] = 0x9927024, c[1c] = 0x5d
&c[d ] = 0x9927015, c[d ] = 0x4e          &c[1d] = 0x9927025, c[1d] = 0x5e
&c[e ] = 0x9927016, c[e ] = 0x4f          &c[1e] = 0x9927026, c[1e] = 0x5f
&c[f ] = 0x9927017, c[f ] = 0x50          &c[1f] = 0x9927027, c[1f] = 0x60
&data[0] = 0x9927008	data[0] = 0x9927010	data[0] should be 0x9927010
&data[1] = 0x992700c	data[1] = 0x9927018	data[1] should be 0x9927018
&data[0][0] = 0x9927010	data[0][0] = 4c4b4a49
&data[0][1] = 0x9927014	data[0][1] = 504f4e4d
&data[1][0] = 0x9927018	data[1][0] = 54535251
&data[1][1] = 0x992701c	data[1][1] = 58575655
&c[0 ] = 0x9927008, c[0 ] = 0x10          &c[10] = 0x9927018, c[10] = 0x51
&c[1 ] = 0x9927009, c[1 ] = 0x70          &c[11] = 0x9927019, c[11] = 0x52
&c[2 ] = 0x992700a, c[2 ] = 0x92          &c[12] = 0x992701a, c[12] = 0x53
&c[3 ] = 0x992700b, c[3 ] = 0x9           &c[13] = 0x992701b, c[13] = 0x54
&c[4 ] = 0x992700c, c[4 ] = 0x18          &c[14] = 0x992701c, c[14] = 0x55
&c[5 ] = 0x992700d, c[5 ] = 0x70          &c[15] = 0x992701d, c[15] = 0x56
&c[6 ] = 0x992700e, c[6 ] = 0x92          &c[16] = 0x992701e, c[16] = 0x57
&c[7 ] = 0x992700f, c[7 ] = 0x9           &c[17] = 0x992701f, c[17] = 0x58
&c[8 ] = 0x9927010, c[8 ] = 0x49          &c[18] = 0x9927020, c[18] = 0x59
&c[9 ] = 0x9927011, c[9 ] = 0x4a          &c[19] = 0x9927021, c[19] = 0x5a
&c[a ] = 0x9927012, c[a ] = 0x4b          &c[1a] = 0x9927022, c[1a] = 0x5b
&c[b ] = 0x9927013, c[b ] = 0x4c          &c[1b] = 0x9927023, c[1b] = 0x5c
&c[c ] = 0x9927014, c[c ] = 0x4d          &c[1c] = 0x9927024, c[1c] = 0x5d
&c[d ] = 0x9927015, c[d ] = 0x4e          &c[1d] = 0x9927025, c[1d] = 0x5e
&c[e ] = 0x9927016, c[e ] = 0x4f          &c[1e] = 0x9927026, c[1e] = 0x5f
&c[f ] = 0x9927017, c[f ] = 0x50          &c[1f] = 0x9927027, c[1f] = 0x60