ibadia
ibadia

Reputation: 919

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. 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.

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("");
}

Upvotes: 0

Views: 170

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

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

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

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 180103

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).

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:

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

Note especially the latter.

Now, function row() is declared like so:

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:

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.

When in function row1() you have ...

    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().

If indeed you want to print a representation of the pointers themselves, then you could do it in row1() like so:

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.

Alternatively, you could rewrite row1() like so:

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

And call it from main() like so:

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.

Upvotes: 2

Related Questions