Ç - 动态内存 [英] c - dynamic memory
问题描述
大家好那里的第一个感谢你们所有的人提供帮助。
现在我想知道的双指针。我在做code是这样的:
Hi everyone out there very first thanks to all of you for providing help. Now i want to know about double pointer. I'm doing code like this:
int main()
{
int **a;
a = (int*)malloc(sizeof(*int)*5);
for (i=0;i<5;i++)
{
a[i] = malloc(sizeof(int)*3);
}
}
现在我不知道如果我这样做是正确的。我怎样才能把值在这种类型的数组?任何人都可以解释这个概念,例子吗?先谢谢了。
Now i dont know if I'm doing it right. How can I put values in this type of array ? Can anybody explain this concept with example ? Thanks in advance.
推荐答案
好了,你已经分配的数组大小等于:
Well, you have allocated an array equivalent in size to:
int a[5][3];
所以输入值你只是这样做:
So to enter values you just do like this:
a[0][0] = 1234;
哪个将使一个值到第一行的第一列和
Which would put a value into the first column of the first row and
a[4][2] = 9999;
将使另一个值到最后一行的最后一列。
would put another value into the last column of the last row.
由于您使用malloc,你也应该通过[I]从i = 0〜4,和免费(A [])循环;
然后免费(一);
或你的程序将导致内存泄漏
Since you are using malloc, you should also loop through a[i] from i = 0 to 4, and free(a[i]);
and then free(a);
or your program will leak memory.
这篇关于Ç - 动态内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!