How to allocate memory to int (*a)[2]?

好久不见. 提交于 2019-12-08 08:24:52

问题


How do I allocate memory to integer type (*a)[2] ? I am comfortable doing it with **a, *a etc. etc., also *a[2], but this looks different.

Can somebody help out ? Thanks in advance.


回答1:


Same as for any pointer type, say you have

int (*a)[2];

a pointer a to arrays of 2 int, then you allocate

a = malloc(number_of_rows * sizeof *a);

to get a block of number_of_rows * (2 * sizeof (int)) bytes.

You then access that with

a[i][j]

with 0 <= i < number_of_rows and 0 <= j < 2.



来源:https://stackoverflow.com/questions/16474366/how-to-allocate-memory-to-int-a2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!