Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C中的数组中获取值?_C_Arrays_Pointers - Fatal编程技术网

如何从C中的数组中获取值?

如何从C中的数组中获取值?,c,arrays,pointers,C,Arrays,Pointers,我有这样一个二维数组: void getC(int **p) { *p = &c[0][0]; } int c[10][10]; int *a; getC(a); a[0][0]; it says error: no match for 'operator[]' in `a[0][0];` what is the problem and how to fix it? 你正在用C++编译器编译C程序。小心 您需要将c的定义置于getC函数之上(或提供一个转发声明) 在函数

我有这样一个二维数组:

void getC(int **p)
{
   *p = &c[0][0];
}

int c[10][10];

int *a;

getC(a);

a[0][0];

it says error: no match for 'operator[]' in `a[0][0];` what is the problem and how to fix it?
<>你正在用C++编译器编译C程序。小心

  • 您需要将
    c
    的定义置于
    getC
    函数之上(或提供一个转发声明)

  • 在函数之外有语句,这在C中是不允许的。用
    intmain(void){…}
    包装
    int*a
    和后续行

  • 您需要一个
    &
    使您的
    getC()
    调用合法-您正在传递一个
    int*
    ,但它需要
    int**

    getC(&a);
    
  • 语句
    a[0][0]
    无效,而且无论如何都是错误的,因为
    a
    只是一个
    int*
    ;你不能取消引用它两次

  • 你可能应该买一本C初学者的书并开始学习

    <>你正在用C++编译器编译C程序。小心

  • 您需要将
    c
    的定义置于
    getC
    函数之上(或提供一个转发声明)

  • 在函数之外有语句,这在C中是不允许的。用
    intmain(void){…}
    包装
    int*a
    和后续行

  • 您需要一个
    &
    使您的
    getC()
    调用合法-您正在传递一个
    int*
    ,但它需要
    int**

    getC(&a);
    
  • 语句
    a[0][0]
    无效,而且无论如何都是错误的,因为
    a
    只是一个
    int*
    ;你不能取消引用它两次


  • 您可能需要一本初级C语言书并开始学习它。

    本质上,您只是通过返回2dim数组第一个元素的地址,将数组/指针从
    int(*)[10]
    (指向10 int数组的指针)降级为简单的
    int指针。虽然这在技术上是正确的(2dim数组的一个元素的地址当然是int*),但数组中int的结构/布局信息丢失了,因此生成的a-ptr现在与int是[10][10]结构的一部分这一事实无关

    在您的情况下,到达数组元素的唯一方法是通过整数数组进行乘法,根据您自己的知识,地址a有100个整数,按10x10组织:

       int *a;
       getC(&a);
       ...= a[10*x + y];  // equivalent of   c[x][y];
    

    但是,从本质上讲,正确的方法(完全保留类型)是

    int c[10][10];
    
    void getC(int (**p)[10])  // pointer to pointer to array of 10 ints 
    {
       *p = c;  // c itself can seamlessly change into a pointer to one sub-element 
                // (i.e. pointer to array of 10)
    }
    
    int main()
    {
        int (*a)[10]; // pointer to array(s) of 10 ints
        int q;
    
        getC(&a);
    
    
        q= a[9][9];
            ...
    }
    
    int c[10][10];
    
    void getC(int (**p)[10][10])  // pointer to pointer to array of 10x10 ints 
    {
       *p = &c;  // &c can seamlessly change into a pointer to 10x10 ints
    }
    
    int main()
    {
        int (*a)[10][10]; // pointer to array(s) of 10x10 ints
        int q;
    
        getC(&a);  // pass adress of pointer to 10x10 ints
    
    
        q= (*a)[9][9];  // need *a in brackets to derference the pointer (operator precedence)
            ...
    }
    
    同样,还有一个维度级别(可能是最直观的解决方案):

    但是,从本质上讲,正确的方法(完全保留类型)是

    int c[10][10];
    
    void getC(int (**p)[10])  // pointer to pointer to array of 10 ints 
    {
       *p = c;  // c itself can seamlessly change into a pointer to one sub-element 
                // (i.e. pointer to array of 10)
    }
    
    int main()
    {
        int (*a)[10]; // pointer to array(s) of 10 ints
        int q;
    
        getC(&a);
    
    
        q= a[9][9];
            ...
    }
    
    int c[10][10];
    
    void getC(int (**p)[10][10])  // pointer to pointer to array of 10x10 ints 
    {
       *p = &c;  // &c can seamlessly change into a pointer to 10x10 ints
    }
    
    int main()
    {
        int (*a)[10][10]; // pointer to array(s) of 10x10 ints
        int q;
    
        getC(&a);  // pass adress of pointer to 10x10 ints
    
    
        q= (*a)[9][9];  // need *a in brackets to derference the pointer (operator precedence)
            ...
    }
    

    本质上,您只是通过返回2dim数组的第一个元素的地址,将数组/指针从
    int(*)[10]
    (指向10 int数组的指针)降级为简单的
    int指针。虽然这在技术上是正确的(2dim数组的一个元素的地址当然是int*),但数组中int的结构/布局信息丢失了,因此生成的a-ptr现在与int是[10][10]结构的一部分这一事实无关

    在您的情况下,到达数组元素的唯一方法是通过整数数组进行乘法,根据您自己的知识,地址a有100个整数,按10x10组织:

       int *a;
       getC(&a);
       ...= a[10*x + y];  // equivalent of   c[x][y];
    

    但是,从本质上讲,正确的方法(完全保留类型)是

    int c[10][10];
    
    void getC(int (**p)[10])  // pointer to pointer to array of 10 ints 
    {
       *p = c;  // c itself can seamlessly change into a pointer to one sub-element 
                // (i.e. pointer to array of 10)
    }
    
    int main()
    {
        int (*a)[10]; // pointer to array(s) of 10 ints
        int q;
    
        getC(&a);
    
    
        q= a[9][9];
            ...
    }
    
    int c[10][10];
    
    void getC(int (**p)[10][10])  // pointer to pointer to array of 10x10 ints 
    {
       *p = &c;  // &c can seamlessly change into a pointer to 10x10 ints
    }
    
    int main()
    {
        int (*a)[10][10]; // pointer to array(s) of 10x10 ints
        int q;
    
        getC(&a);  // pass adress of pointer to 10x10 ints
    
    
        q= (*a)[9][9];  // need *a in brackets to derference the pointer (operator precedence)
            ...
    }
    
    同样,还有一个维度级别(可能是最直观的解决方案):

    但是,从本质上讲,正确的方法(完全保留类型)是

    int c[10][10];
    
    void getC(int (**p)[10])  // pointer to pointer to array of 10 ints 
    {
       *p = c;  // c itself can seamlessly change into a pointer to one sub-element 
                // (i.e. pointer to array of 10)
    }
    
    int main()
    {
        int (*a)[10]; // pointer to array(s) of 10 ints
        int q;
    
        getC(&a);
    
    
        q= a[9][9];
            ...
    }
    
    int c[10][10];
    
    void getC(int (**p)[10][10])  // pointer to pointer to array of 10x10 ints 
    {
       *p = &c;  // &c can seamlessly change into a pointer to 10x10 ints
    }
    
    int main()
    {
        int (*a)[10][10]; // pointer to array(s) of 10x10 ints
        int q;
    
        getC(&a);  // pass adress of pointer to 10x10 ints
    
    
        q= (*a)[9][9];  // need *a in brackets to derference the pointer (operator precedence)
            ...
    }
    

    您没有将2d数组传递给getC。你是说int**a吗?这是什么?实际代码在哪里?@Nirk我的意思是int*a;您正在丢失指针指向2维数组的信息,只需将其寻址为int ptr即可。请参阅下面的示例。您没有将2d数组传递给getC。你是说int**a吗?这是什么?实际代码在哪里?@Nirk我的意思是int*a;您正在丢失指针指向2维数组的信息,只需将其寻址为int ptr即可。请参阅下面的示例。是的,它应该作为getC(&a);,但是如何使用指针a获取值呢?那么您可能需要一个指向数组的指针,而不是一个简单的
    int*
    pointer。是的,它应该作为getC(&a);,但是如何使用指针a获取值呢?那么您可能需要一个指向数组的指针,而不是一个简单的
    int*
    pointer。好的,谢谢,但是当我们声明无效的getC(int(**p)[10])时,我们是否必须指定为:int(**p)[10]?我们可以将其声明为:getC(int**p)吗?如果您将其声明为“int**p”,则只能传递一个“int**”:例如int*a;getC&a)。有了这个“a”,你就不能访问二维的值。我的答案中的第一个灰色框是访问数组成员的方法(作为一维)。唯一的替代方案将涉及参数的野生类型转换。好的,谢谢,但是当我们声明void getC(int(**p)[10])时,我们是否必须指定为:int(**p)[10]?我们可以将其声明为:getC(int**p)吗?如果您将其声明为“int**p”,则只能传递一个“int**”:例如int*a;getC&a)。有了这个“a”,你就不能访问二维的值。我的答案中的第一个灰色框是访问数组成员的方法(作为一维)。唯一的替代方案将涉及参数的野生类型转换。