4

sizeof 计算问题求解

 2 years ago
source link: https://www.v2ex.com/t/789496
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  C

sizeof 计算问题求解

  JQiue · 1 小时 17 分钟前 · 244 次点击
void foo(int *arr) {
  printf("%d\n", sizeof arr); // 4
}

int main(){
  int arr[] = {1, 2, 3, 4, 5};
  printf("%d\n", sizeof arr); // 20
  foo(arr);
}

对于上述代码的sizeof计算数组字节结果有些不太明白,arr很显然和arr[0]是等效的,为什么在主函数中计算arr的大小等于20(所有元素加起来的字节大小),而作为参数传递给另一个函数时却是4


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK