简体   繁体   English

将数组指针传递给C中的函数

[英]passing array pointer to function in C

In my main() function I initialize the array pointer *AD and copy the address of array A[5] into pointer AD[5] using for loop. 在我的main()函数中,我初始化数组指针*AD并使用for循环将数组A[5]的地址复制到指针AD[5] When I pass this pointer AD to function row1() and assign all of its values (in AD) to I[5] I am getting the real values of A[5] instead of the address. 当我将此指针AD传递给函数row1()并将其所有值(在AD中)分配给I[5]我得到的是A[5]的实际值而不是地址。 On the other hand, when i print the value of the AD[5] pointer in main() I get the address. 另一方面,当我在main()打印AD[5]指针的值时,我得到了地址。 I am using DEV c++ compiler. 我正在使用DEV c ++编译器。

Why in the function am I getting real values? 为什么在函数中我得到真实的价值?

#include <stdio.h>
int main(void)
{
    int test;
    int A[5];
    int *AD[5];
    FILE *fpc;
    fpc=fopen("testing.txt","r");
    fscanf(fpc,"%d",&test);
    int i;
    for(i=0;i<test;i++)
    {
        int j;
        for(j=0;j<5;j++)
        {
            fscanf(fpc,"%d",&A[j]);
            AD[j]=&A[j];
            printf("%d   ",AD[j]);

        }
        puts("");

        row1(AD[0]);    
    }
}

void row1 (int AD[0])
{
    int I[5];
    int i;
    for(i=0;i<5;i++)
    {
        I[i]=AD[i];
        AD[i]=AD[i]+1;
        printf("%d,        ",I[i]);
    }   
    puts("");
    puts("");
    puts("");
    puts("");
}

If you want to pass an array of pointers then the function declaration and its call will look like 如果要传递一个指针数组,则函数声明及其调用将类似于

void row1 (int *AD[] );

//...

row1( AD );    

Take into account that the function must be declared before its call. 考虑到必须在调用函数之前声明该函数。

As for the function itself then it is not clear what you are trying to achieve within the function. 至于功能本身,则不清楚您要在功能内实现什么。

Also as array AD is an array of pointers then you should use format specifier %p in printf function 同样因为数组AD是指针数组,所以您应该在printf函数中使用格式说明符%p

printf("%p   ",AD[j]);

The declaration ... 声明...

int *AD[5];

... declares AD to be an array of five pointers to int . ...声明AD为五个指向int指针的数组。 You initialize each member with a pointer to the corresponding member of A (which seems wasteful, but whatever). 您可以使用指向A的相应成员的指针来初始化每个成员(这看起来很浪费,但是不管用什么)。

You later perform this call: 您稍后执行此调用:

    row1(AD[0]);    

That's fine: it passes the first element of AD to the function. 很好:它将AD的第一个元素传递给函数。 Since AD[0] is a pointer to A[0] , that's equivalent to either of these: 由于AD[0]是指向A[0]的指针,所以它们等效于以下任意一个:

    row1(&A[0]);    
    /* or */
    row1(A);    

Note especially the latter. 特别要注意后者。

Now, function row() is declared like so: 现在,函数row()的声明如下:

void row1 (int AD[0])

That appears to declare function parameter AD as an array of zero int s, which would be invalid, but in fact, array lengths in function argument declarations are ignored. 这似乎将函数参数AD声明为一个零个int s的数组,这将是无效的,但实际上,函数参数声明中的数组长度会被忽略。 That declaration is equivalent to either of these: 该声明等效于以下任何一个:

void row1 (int AD[])
/* or */
void row1 (int *AD)

That's consistent with the argument you are passing, but note that the AD in function row1() is distinct from the AD in main() , and they have different and incompatible types . 这是与你逝去的说法一致,但要注意, AD的功能row1()是区别于ADmain() 他们有不同的和不兼容的类型

When in function row1() you have ... 在功能row1()您有...

    I[i]=AD[i];

... you are assigning the value pointed to by pointer AD + i . ...您将值由指针指向 AD + i The way you set things up, this works out to the value that could be expressed as A[i] in main() . 设置方式的方式,可以得出可以在main()表示为A[i]的值。

If indeed you want to print a representation of the pointers themselves, then you could do it in row1() like so: 如果确实要打印指针本身的表示形式,则可以在row1()这样做,如下所示:

int i;
for (i = 0; i < 5; i++) {
    printf("%p,        ", AD++);
}

Note that it's the pointer itself being printed, not the thing it refers to. 请注意,它是指针本身被打印,而不是它所指的东西。 Note also that that depends for its correctness on the manner in which main() initializes (its) AD . 还请注意,这取决于它的正确性,取决于main()初始化(其) AD

Alternatively, you could rewrite row1() like so: 另外,您可以像这样重写row1()

void row1 (int *AD[])
{
    int i;
    for(i = 0; i < 5; i++)
    {
        printf("%p,        ", AD[i]);
    }   
}

And call it from main() like so: 然后从main()调用它,如下所示:

row1(AD);

Note there that you are passing the whole array, not just a single element. 请注意,您传递的是整个数组,而不仅仅是单个元素。 That's your only alternative if you cannot rely on the members of main() 's AD to be pointers to consecutive members on an array. 如果您不能依靠main()AD成员作为指向数组中连续成员的指针,那是唯一的选择。

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

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