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("");
}
int A[5]; ... fscanf(fpc,"%d",&A[j]);; INCORRECT:AD[j]=&A[j]; BETTER:AD = A; k = AD[j];... OR ... (if you must)AD = &A[j]; k = AD[0];"%p"in order to print pointers (AD[j]in the case above). Your code will truncate the printed values on 64-bit platforms (or more generally, as SO users like to put it, your code yields undefined behavior).