c - 理解指针数组

标签 c pointers

我正在做这样的事情;

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) 等值?

最佳答案

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);

关于c - 理解指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59735011/

相关文章:

c - 为字符数组的strlen分配空间时为什么要加1?

asp.net - 从 ASP.NET 应用程序调用时,编译的 C dll 无法设置指针

pointers - 将结构作为参数传递给函数进行修改的内存/处理器效率最高的方法是什么?

c++ - 将 strcpy() 与指向 cstring 的指针一起使用时出现段错误

c++ - 将指针从 C 传递到 C++,反之亦然

java - 比较 float 及其副本

从指针转换为不同大小的整数警告

c++ - 在线程之间共享变量的方法

c++ - 有没有办法返回 null 而不是自定义对象(不是指针)?

c - 为什么我不能用动态分配的数组覆盖数组(传递给另一个函数)?