Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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,假设我在main中有一个指向结构的双指针(即指向结构的指针数组),我想将它传递给一个函数,在那里它将被填充。假设我想将每个“输出”传递给前面带有“&”的函数,以便清楚哪些参数是输出。这意味着在函数中我必须处理一个三重指针 void fillarray(int elements, struct MyStruct *** array, int * output) { // fill the array // (see below) // return output

假设我在main中有一个指向结构的双指针(即指向结构的指针数组),我想将它传递给一个函数,在那里它将被填充。假设我想将每个“输出”传递给前面带有“&”的函数,以便清楚哪些参数是输出。这意味着在函数中我必须处理一个三重指针

void fillarray(int elements, struct MyStruct *** array, int * output) {
    // fill the array
        // (see below)
    // return output
    *output = ...
}

int main() {
    // some code
    // ...

    int elements_count = 10;
    int some_int_output;

    struct MyStruct ** = malloc(elements_count * sizeof(struct MyStruct *));
    for (i = 0; i < elements_count; i++) {
        array_of_pointers[i] = malloc(sizeof(struct MyStruct));

    fillarray(elements_count, &array_of_pointers, &some_int_output);

    // some code
    // ...
    }
我认为问题与首先解除引用哪个“指针维度”有关,这样
*array[I]
就不会访问预期的内存,而
*(*array+I)
会访问。使用第二种表示法会使事情变得更复杂,更容易出错,从而抵消了在函数调用中使用“&”的(假定的)优势

有没有人建议如何更好地重写它,以便在函数中使用数组表示法,同时保留三重指针?有没有办法影响“指针维度顺序”

提前谢谢


PS我知道还有(许多)其他方法可以达到同样的效果,我已经成功地使用了其中的一些方法。在这里,我想知道是否可以满足上述条件,同时了解更多关于指针的信息。

不,这与运算符优先级有关

*array[i]
不等于

(*array)[i]
而是

*(array[i]) => *(*(array + i))
因为数组下标运算符的优先级高于间接运算符,所以添加括号可以使第一个表达式工作

请注意,对于
i==0

*(array[i]) => *(*(array + i))
意味着


因此,首先计算哪个运算符没有实际的区别,但正如
i>0
那样,会有区别。

不,它与运算符优先级有关,这是

*array[i]
不等于

(*array)[i]
而是

*(array[i]) => *(*(array + i))
因为数组下标运算符的优先级高于间接运算符,所以添加括号可以使第一个表达式工作

请注意,对于
i==0

*(array[i]) => *(*(array + i))
意味着

因此,首先计算哪个运算符没有实际差异,但随着
i>0
的变化,会出现差异。

解决方案是:

for (i = 0; i < elements; i++) {
    *(*array + i) = ... // WORKS FOR ALL ELEMENTS
}
struct MyStruct **arr = *array;
现在,您可以像使用普通双指针一样使用arr。

解决方案是:

void fillarray(int elements, struct MyStruct *** array, int * output) {
    // fill the array
        // (see below)
    // return output
    *output = ...
}

int main() {
    // some code
    // ...

    int elements_count = 10;
    int some_int_output;

    struct MyStruct ** = malloc(elements_count * sizeof(struct MyStruct *));
    for (i = 0; i < elements_count; i++) {
        array_of_pointers[i] = malloc(sizeof(struct MyStruct));

    fillarray(elements_count, &array_of_pointers, &some_int_output);

    // some code
    // ...
    }
for (i = 0; i < elements; i++) {
    *(*array + i) = ... // WORKS FOR ALL ELEMENTS
}
struct MyStruct **arr = *array;

现在您可以像使用普通双指针一样使用
arr

好的,符号
(*array)[i]
确实有效。我认为这是一个可以接受的权衡。在处理***时只需记住添加括号。好的,符号
(*array)[i]
确实有效。我认为这是一个可以接受的权衡。在处理***时只需记住添加括号。这也是一个有趣的解决方案,如果我们在函数中进行大量计算,它可能是值得的。@m2oTech这个解决方案实际上更优雅,我只是没有提到它,因为我试图解释代码失败的原因。但是我会选择这个解决方案,它使问题更清楚,@iharob,我认为你的解决方案更直接地解决了这个问题(这实际上只是一个正式的问题)。我也觉得这个解决方案很优雅,我可能也更喜欢它,同样也不太容易出错。这也是一个有趣的解决方案,如果我们在函数中进行大量计算,它可能是值得的。@m2oTech这个解决方案实际上更优雅,我只是没有提到它,因为我试图解释代码失败的原因。但是我会选择这个解决方案,它使问题更清楚,@iharob,我认为你的解决方案更直接地解决了这个问题(这实际上只是一个正式的问题)。我也觉得这个解决方案很优雅,我可能也更喜欢它,同样也不容易出错。
void fillarray(int elements, struct MyStruct *** array, int * output) {
    // fill the array
        // (see below)
    // return output
    *output = ...
}

int main() {
    // some code
    // ...

    int elements_count = 10;
    int some_int_output;

    struct MyStruct ** = malloc(elements_count * sizeof(struct MyStruct *));
    for (i = 0; i < elements_count; i++) {
        array_of_pointers[i] = malloc(sizeof(struct MyStruct));

    fillarray(elements_count, &array_of_pointers, &some_int_output);

    // some code
    // ...
    }