2011年2月22日星期二

about the pointer

after reviewing Fardad's lecture, i have a question about the address.

#include <stdio.h>
int main(){
  int a[3][5]={
                {10,20,30,40,50},
                {100,200,300,400,500},
                {1000,2000,3000,4000,5000}
              };
  printf("%u\n", a);
  printf("%u\n", a+1);
  printf("%u\n", a[0]);
  printf("%u\n", a[0]+1);
  printf("%d, %d\n", *(*(a+2) + 3), a[2][3]);
  return 0;
}

in my opinion, the first one "a" is the same as the third one "a[0]". so they have the same address.
but is it the second one "a+1" = "a[0]+1"????

the output is:
2030780
2030800
2030780
2030784
4000, 4000

i don't know why???

2 条评论:

  1. One integer is 4 bytes. But since it's two dimensional array, we have to multiply 4 (bytes) by 5 (comes from [3][5] array we created) which is equal to 20 bytes. So 'a+1' means "move to memory by 20 bytes". a[0]+1 is different because we're only referring to a specific array in the two dimensional array. So.. 'a[0]+1' means "move to memory by 4 bytes". You can also check the memory address, 2030800-2030780=20.

    回复删除