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

时间:2021-12-28 21:42:13

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. 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. On the other hand, when i print the value of the AD[5] pointer in main() I get the address. I am using DEV c++ compiler.

在我的main()函数中,我初始化数组指针* AD并使用for循环将数组A [5]的地址复制到指针AD [5]中。当我将此指针AD传递给函数row1()并将其所有值(在AD中)分配给I [5]时,我得到A [5]的实际值而不是地址。另一方面,当我在main()中打印AD [5]指针的值时,我得到了地址。我正在使用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("");
}

2 个解决方案

#1


The declaration ...

声明......

int *AD[5];

... declares AD to be an array of five pointers to int. You initialize each member with a pointer to the corresponding member of A (which seems wasteful, but whatever).

...声明AD是一个由五个指向int的数组。您使用指向A的相应成员的指针初始化每个成员(这似乎很浪费,但无论如何)。

You later perform this call:

您稍后执行此调用:

    row1(AD[0]);    

That's fine: it passes the first element of AD to the function. Since AD[0] is a pointer to A[0], that's equivalent to either of these:

没关系:它将AD的第一个元素传递给函数。由于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 ints, which would be invalid, but in fact, array lengths in function argument declarations are ignored. That declaration is equivalent to either of these:

这似乎将函数参数AD声明为零整数的数组,这将是无效的,但事实上,函数参数声明中的数组长度被忽略。该声明相当于以下任何一种:

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.

这与您传递的参数一致,但请注意函数row1()中的AD与main()中的AD不同,并且它们具有不同且不兼容的类型。

When in function row1() you have ...

在函数row1()中你有......

    I[i]=AD[i];

... you are assigning the value pointed to by pointer AD + i. The way you set things up, this works out to the value that could be expressed as A[i] in main().

...您正在指定指针AD + i指向的值。你设置的方式,这可以在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成员指向数组上的连续成员,那么这是您唯一的选择。

#2


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

#1


The declaration ...

声明......

int *AD[5];

... declares AD to be an array of five pointers to int. You initialize each member with a pointer to the corresponding member of A (which seems wasteful, but whatever).

...声明AD是一个由五个指向int的数组。您使用指向A的相应成员的指针初始化每个成员(这似乎很浪费,但无论如何)。

You later perform this call:

您稍后执行此调用:

    row1(AD[0]);    

That's fine: it passes the first element of AD to the function. Since AD[0] is a pointer to A[0], that's equivalent to either of these:

没关系:它将AD的第一个元素传递给函数。由于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 ints, which would be invalid, but in fact, array lengths in function argument declarations are ignored. That declaration is equivalent to either of these:

这似乎将函数参数AD声明为零整数的数组,这将是无效的,但事实上,函数参数声明中的数组长度被忽略。该声明相当于以下任何一种:

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.

这与您传递的参数一致,但请注意函数row1()中的AD与main()中的AD不同,并且它们具有不同且不兼容的类型。

When in function row1() you have ...

在函数row1()中你有......

    I[i]=AD[i];

... you are assigning the value pointed to by pointer AD + i. The way you set things up, this works out to the value that could be expressed as A[i] in main().

...您正在指定指针AD + i指向的值。你设置的方式,这可以在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成员指向数组上的连续成员,那么这是您唯一的选择。

#2


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