Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C语言指针陷阱 #29

Open
rainyear opened this issue Apr 4, 2016 · 0 comments
Open

C语言指针陷阱 #29

rainyear opened this issue Apr 4, 2016 · 0 comments
Assignees

Comments

@rainyear
Copy link
Owner

rainyear commented Apr 4, 2016

最近重新拾起C语言,Python 用得久了对指针的操作已经非常生疏了,在这里对遇到的一些指针操作上的陷阱进行整理记录。

1. 数组初始化导致指针重叠

在初始化数组的时候遇到后一个数组的指针与前一个重叠:

  size_t n = 6;
  size_t f1[n];
  size_t f2[n];
  size_t ls[2][n-1];

  printf("&ls[1][5] = %p\n&f2[0] = %p", &ls[1][5], f2);

输出结果为:

&ls[1][5] = 0x7fff501f3730
&f2[0] = 0x7fff501f3730

用 malloc 动态分配或者将 size_t f2[n] 放到 ls 声明的后面不会有问题,那上面出现这个问题的原因是什么呢?

原因


2. 二维数组只声明未初始化

@rainyear rainyear self-assigned this Sep 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant