Why address of the variable changes after passing the function

I need to find the smallest and biggest number in the array using pointers; in addition to that, I also need to output the address of these numbers in the main function.

The part I am struggling with is finding the address of the number. The problem is that there are different addresses for the same number. Why is it so?

This is what I get as output:

Please enter a value 0 - 4
Please enter a value 1 - 7
Please enter a value 2 - 2

0x7ffeefbff4fc,4
0x7ffeefbff500,7
0x7ffeefbff504,2
The min value is: 2
The max value is: 7
#define size 3

void Input (int arr[]);
void AdressOutput (int arr[]);
void MinAndMax (int arr[],int *min,int *max);

int main(void)
{
int arr[size];
int min=0,max=0;

Input(arr);
MinAndMax(arr,&min,&max);
printf ("The min value is: %d\n",min);
printf ("The max value is: %d\n and address %p\n",max,&max);

return 0;
}
void Input (int arr[])
{
int i;
int *p=arr;
for (i=0;i<size;i++)
{
printf ("Please enter a value %d - ",i);
scanf ("%d",(p+i));
}
printf ("\n");

}
void AdressOutput (int arr[])
{
int i=0;
int *p=arr;
for (i=0;i<size;i++)
{
printf ("%p,%d\n",(p+i),*(p+i));
}
}
void MinAndMax (int arr[],int *min,int *max)
{
int i=0;
int *p;
p=arr;
*min=*p;
*max=*p;
for (i=0;i<size;i++)
{
if(*(p+i)>*max) //finding max
*max=*(p+i);
}

for (i=0;i<size;i++)
{
if(*(p+i)<*min)//finding min
*min=*(p+i);
}
}

I don't understand why the address changes and how can I create a function which will find addresses and will allow me to print them out in the main function?

• answered 2019-09-16 02:31

In the function MinAndMax arr is your input parameter (parameter given to a function for input) and max and min are output parameters (parameter given to a function for storing output). the function would get the addresses of where to store output from output parameters. output parameters are always addresses, ie the value in these parameters point to the memory where you ultimately want to store your output.

What do you want to store? do you want to store an int OR do you want an address which points to int

you want to print the address of min and max values, so you want to store addresses which points to int

in main function you have declared int min=0,max=0;, these can't store addresses, thay can store only int. so lets change that to int *min_address, *max_address;

you have declared function MinAndMax as void MinAndMax (int arr[],int *min,int *max);. for parameters min and max this means addresses which point to int.

but as you want to store addresses, so what you want in your function is: addresses of memory cells which store the address which points to int. in other words you want a double pointer. so lets change the function declaration to void MinAndMax (int arr[], int **min_address, int **max_address);

here is the corrected code:

#include<stdio.h>

#define size 3

void Input (int arr[]);
void AdressOutput (int arr[]);
void MinAndMax (int arr[], int **min_address, int **max_address);

int main(void)
{
int arr[size];

Input(arr);
printf("The min value is: %d\n and adress %p\n", *min_address, min_address);
printf("The max value is: %d\n and adress %p\n", *max_address, max_address);

return 0;
}
void Input (int arr[])
{
int i;
int *p=arr;
for (i=0;i<size;i++)
{
printf("Please enter a value %d - ",i);
scanf("%d",(p+i));
}
printf ("\n");

}
void AdressOutput (int arr[])
{
int i=0;
int *p=arr;
for (i=0;i<size;i++)
{
printf("%p,%d\n",(p+i),*(p+i));
}
}
void MinAndMax (int arr[], int **min_address, int **max_address)
{
int i=0;
int *p;
p=arr;