Beginner question. I know a bit about pointers but I am having a lot of trouble trying to print values of an array using pointers in a function. Particularly at this line: printf("\n%d\t\t%d\t0x%X", i+1, *(arr+i) , arr); My output is a table that displays input count, value entered by user, and the address it is stored. The address looks fine but the values print garbage. I thought the syntax *(arr+i) is correct when trying to increment array of pointers with index i??
#include<stdio.h>
#include <stdlib.h>
void function(int *arr[]);
main()
{
int array[5];
function(&array);
}
void function(int *arr[])
{
int i, n;
printf("How many elements? ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter element %d: ", i);
scanf("%d", arr);
arr++;
}
printf("\nInput\t\tValue\tAddress");
for(i=0; i<n; i++)
{
printf("\n%d\t\t%d\t0x%X", i+1, *(arr+i) , arr);
arr++;
}
}
int *arr[]is equivalent toint **arr, a pointer to a (array of) pointer. The compiler should have generated a warning at the call, since&arrayis of typeint *.