简体   繁体   English

多维数组对C的理解

[英]Multidimensional arrays understanding for c

I have a question regarding passing an array to another function. 我有一个关于将数组传递给另一个函数的问题。

#include <stdio.h>
void print (int ar[]);

main () {
    int array[2][2] = {1,2,3,4};
    print(array);
}

void print (int ar[]) {
    printf("%d\n", ar[0]);
    printf("%d\n", *(ar+1));
}

So let's say if i'm passing array to function print. 因此,如果我将数组传递给函数print的话。 Am I passing address of the first 1-D array (array[0]) or am i just passing the address of the very first variable (array[0][0]). 我是传递第一个一维数组的地址(array [0])还是刚传递了第一个变量的地址(array [0] [0])。

And as for the print function, in this case, am I correct to say that by printing ar[0], I am printing array[0][0]? 至于打印功能,在这种情况下,我是否正确地说是通过打印ar [0]来打印array [0] [0]?

But why is it that for the case of printing *(ar+1), I am actually printing array[0][1] instead of array[1][0]? 但是,为什么在打印*(ar + 1)的情况下,我实际上是在打印array [0] [1]而不是array [1] [0]?

Thank you everyone in advance! 提前谢谢大家!

If you are passing a two-dimensional array to a function: 如果要将二维数组传递给函数:

int array[NROWS][NCOLUMNS];
print(array);

the function's declaration must match: 该函数的声明必须匹配:

void print(int array[][NCOLUMNS])
{ ... }

or 要么

void print(int (*ap)[NCOLUMNS]) /* ap is a pointer to an array */
{ ... }

The 2D arrays which is array of arrays decays a pointer to an array rather than a pointer to a pointer. 作为数组的数组的2D数组会衰减指向数组的指针,而不是指向指针的指针。

So let's say if [I]'m passing array to function print . 因此,假设[I]是否将array传递给函数print Am I passing address of the first 1-D array ( array[0] ) or am [I] just passing the address of the very first variable ( array[0][0] ). 我传递第一个一维数组( array[0] )的地址还是我传递第一个变量( array[0][0] )的地址?

You are passing a pointer to the first element of array , array[0] , which is itself an array. 您正在传递一个指向array array[0]的第一个元素的指针,该元素本身就是一个数组。 That pointer has the same value as a pointer to array[0][0] , but different type. 该指针的值与指向array[0][0]的指针相同,但类型不同。 In fact, your code is incorrect in that the argument's type and the function parameter's type do not agree, but as long as the types int(*)[] and int * have the same representation, the reinterpretation will work in practice. 实际上,您的代码是不正确的,因为参数的类型和函数参数的类型不一致,但是只要类型int(*)[]int *具有相同的表示形式,则重新解释将在实践中起作用。

And as for the print function, in this case, am I correct to say that by printing ar[0] , I am printing array[0][0] ? 至于print功能,在这种情况下,我是否正确地说是通过打印ar[0]打印array[0][0]

Yes, when you call print() as you do in your example, provided that type representations agree as described above. 是的,当您像在示例中那样调用print() ,前提是类型表示形式如上所述。

But why is it that for the case of printing *(ar+1) , I am actually printing array[0][1] instead of array[1][0] ? 但是为什么在打印*(ar+1)的情况下,我实际上是在打印array[0][1]而不是array[1][0]

Because ar is a pointer to the first element of array , ie the 1D array array[0] . 因为ar是指向array的第一个元素的指针,即一维数组array[0] Its elements are array[0][0] , array[0][1] , etc.. ar + 1 is therefore a pointer to the second element of array[0] , which is array[0][1] . 它的元素是array[0][0]array[0][1]等。因此ar + 1是指向array[0]的第二个元素的指针,后者是array[0][1]

You have two options to make your code correct, both of which might help clarify the issues involved. 您有两个选择可以使代码正确,这两个选项都可能有助于弄清所涉及的问题。 You could change the argument you pass to print() to agree with the function: 您可以更改传递给print()的参数以与该函数一致:

print(array[0]);

Alternatively, you could change the declared type of print() 's argument, whith a corresponding alteration to its use: 或者,您可以更改print()参数的声明类型,并对其使用进行相应的更改:

void print(int (*ar)[2]) {
    printf("%d\n", ar[0][0]);
    printf("%d\n", *(ar+1)[0]);
}

or 要么

void print(int ar[][2]) {
    printf("%d\n", ar[0][0]);
    printf("%d\n", *(ar+1)[0]);
}

The latter two change the meaning of *(ar + 1) . 后两个更改*(ar + 1)的含义。

Your code is probably not doing what you want. 您的代码可能没有执行您想要的操作。 Compile with Wall and you will see some warnings. 使用Wall编译,您将看到一些警告。

You need to change print argument to accept a 2D array (now you are passing int (*)[2] but the function is expecting int * ), and then you can print the values correctly. 您需要更改print参数以接受2D数组(现在您正在传递int (*)[2]但函数期望使用int * ),然后可以正确打印值。

#include <stdio.h>
void print (int ar[][2]);

int main () {
    int array[2][2] = {
        {1,2},
        {3,4}
    };
    print(array);
    return 0;
}

void print (int ar[][2]) {
    printf("%d\n", ar[0][0]); // 1
    printf("%d\n", ar[1][0]); // 4
}

When you pass an array to a function, you are using "pass-by-reference" meaning you pass the address of the array. 将数组传递给函数时,使用的是“按引用传递”,这意味着您传递了数组的地址。

  • Am I passing address of the first 1-D array (array[0]) or am i just passing the address of the very first variable (array[0][0]). 我是传递第一个一维数组的地址(array [0])还是刚传递了第一个变量的地址(array [0] [0])。

No. The way you are passing array currently you are giving the function the 2-D array. 否。当前传递数组的方式是为函数提供二维数组。

  • And as for the print function, in this case, am I correct to say that by printing ar[0], I am printing array[0][0]? 至于打印功能,在这种情况下,我是否正确地说是通过打印ar [0]来打印array [0] [0]?

No. You still need to specify the number you want to print from ar[0] 否。您仍然需要指定要从ar [0]打印的数字。

  • But why is it that for the case of printing *(ar+1), I am actually printing array[0][1] instead of array[1][0]? 但是,为什么在打印*(ar + 1)的情况下,我实际上是在打印array [0] [1]而不是array [1] [0]?

This is because you are telling the program to go to array in memory and use pointer arithmetic to traverse the array. 这是因为您要告诉程序转到内存中的数组,然后使用指针算术遍历数组。 For example, if you have a char * str variable that contains characters abcde\\0 and want to get the character at position 2 you can either do str[2] or *(str+2) which means the same thing and both give the character 'c' . 例如,如果您有一个包含字符abcde\\0char * str变量,并且想让该字符位于位置2,则可以执行str[2]*(str+2) ,这意味着相同的事情,并且两者都给出字符'c'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM