首页 > 解决方案 > 了解指针数组

问题描述

我正在做这样的事情;

int main()
{
    int *b[2], j;
    for (j = 0; j < 2; j++)
    {
        b[j] = (int *)malloc(12 * sizeof(int));
    } 
    return 0;
}

请告诉我这条指令的真正含义是什么?以及如何将这个指针数组传递给函数以访问诸如此类的值*(B[0]+1),*(B[1]+1)

标签: cpointers

解决方案


int main(void)
{
    int *b[2], j; // initialization of an array of pointers to integers (size = 2)
    for (j = 0; j < 2; j++) // for each of the pointers 
    {
        b[j] = malloc(12 * sizeof (int)); // allocate some space = 12 times size of integer in bytes (usually 4)
    } 
    return 0;
}

如果您想将此数组传递给函数,您只需传递 b

foo(b);

推荐阅读