pointers - Unknown Output of C code -


how following code give answer 2036, 2036, 2036.

what output of following c code? assume address of x 2000 (in decimal) , integer requires 4 bytes of memory.

int main() {     unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6},                             {7, 8, 9}, {10, 11, 12}};    printf("%u, %u, %u", x+3, *(x+3), *(x+2)+3); } 

as you've found out, people can bit harsh on if don't post crafted code. print addresses, use either %p or macros <inttypes.h> such prixptr , casts uintptr_t.

using %p, code should read:

#include <stdio.h>  int main(void) {      unsigned int x[4][3] =     {         {  1,  2,  3 }, {  4,  5,  6 },          {  7,  8,  9 }, { 10, 11, 12 }     };     printf("%p\n", (void *)x);     printf("%p, %p, %p\n", (void *)(x+3), (void *)(*(x+3)), (void *)(*(x+2)+3));     return 0; } 

now have defined behaviour, though values won't convenient 2000 , 2036.

however, x+3 address of fourth array of 3 int after start of array, assuming sizeof(int) == 4 stated, 36 bytes after start of x — 2036 if x @ 2000.

*(x+3) address of start of fourth array; @ 2036 under same assumptions.

*(x+2)+3 adds 3 address of third array of 3 int , adds 3 more it. ends 36 bytes after start of x, or @ 2036 if x @ 2000.

on 64-bit machine, output reads:

0x7fff5800a440 0x7fff5800a464, 0x7fff5800a464, 0x7fff5800a464 

hex 0x24 36 decimal, of course.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -