Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having problem understanding the array of pointer. The question that I am h

ID: 3623590 • Letter: I

Question

I am having problem understanding the array of pointer. The question that I am having problem with is how to make 2 arrays pointing to one other array.

here is my code.
I am trying to make smallArr, bigArr pointing to arr instead of just copying arr to smallArr, bigArr.

Basiclly this program ask user to enter value for arr, sort them in decending , original, ascending order, with out changing the value in the arr array.

#include<stdio.h>
#include<stdlib.h>

//Function Declarations
int getData (float** arr);
void* copy (float* arr, int num, float** smallArr, float** bigArr);
void SortSmall(float* smallArr,int num);
void SortBig (float* bigArr, int num);
float* smallest (float* pWalker,float* pLast);
float* biggest (float* pWalker,float* pLast);
void exchange (float* pWalker,float* holder);
void outPut (float* arr, float* smallArr, float* bigArr, int num);

int main (void)
{
//Local Declarations
float* arr;
float* smallArr;
float* bigArr;
float* pLast;
int num, i, j;

//Statements
num = getData(&arr);
copy(arr, num, &smallArr, &bigArr);
SortSmall(smallArr, num);
SortBig(bigArr, num);
outPut(arr, smallArr, bigArr, num);


for(i=0; i<num; i++)
{
printf("%10.1f %10.1f %15.1f ",*(smallArr+i),*(arr+i),*(bigArr+i));
}

return 0;
}//End of Main


int getData(float** arr)
{

//Local Declarations
int num;
int i;

//Statements

printf("Enter the number of value want to be input: ");
scanf("%d", &num );

*arr = (float*)calloc(num, sizeof(float));
if (*arr == NULL)
{
printf("Memory overflow ");
exit(100);
}//if

for(i = 0; i < num; i++)
{
printf("Enter the %d number of the array: ", i+1);
scanf("%f", *arr + i);
}//for

return num;
}//getData


void* copy(float* arr, int num, float** smallArr, float** bigArr)
{

//Local Declarations
float temp;
int i, j;

//Statements
*smallArr = (float*)calloc(num, sizeof(float));

if (smallArr == NULL)
{
printf("Memory overflow ");
exit(101);
}//if

*bigArr = (float*)calloc(num, sizeof(float));

if (bigArr == NULL)
{
printf("Memory overflow ");
exit(102);
}//if

for(i=0; i<num;i++)
{
temp = *(arr+i);
*(*smallArr+i) = temp;
*(*bigArr+i) = temp;
}//for

return;
}//copy


void SortSmall(float* smallArr, int num)
{
//Local Declarations
float* pWalker;
float* pSmallest;
float* pLast;
int i;

//Statements
for(pWalker= smallArr, pLast = smallArr+num; pWalker < pLast; pWalker++)
{
pSmallest = smallest(pWalker, pLast);
exchange(pWalker, pSmallest);
}//for

return;
}// SortSmall



void SortBig(float* bigArr, int num)
{
//Local Declarations
float* pWalker;
float* pBiggest;
float* pLast;

//Statements
for(pWalker= bigArr, pLast = bigArr +num; pWalker < pLast; pWalker++)
{
pBiggest = biggest(pWalker, pLast);
exchange(pWalker, pBiggest);

}//for


return;
}//SortBig



float* smallest(float* pWalker,float* pLast)
{
//Local Declarations
float* pLooker;
float* pSmallest;

//Statements
for(pSmallest = pWalker, pLooker = pWalker + 1; pLooker < pLast; pLooker++)
if(*pLooker < *pSmallest)
pSmallest = pLooker;


return pSmallest;
}// smallest



float* biggest(float* pWalker,float* pLast)
{
//Local Declarations
float* pLooker;
float* pBiggest;

//Statements
for(pBiggest = pWalker, pLooker = pWalker + 1; pLooker < pLast; pLooker++)
{
if(*pLooker > *pBiggest)
pBiggest = pLooker;
}
return pBiggest;
}// biggest


void exchange(float* pWalker, float* holder)
{
//Local Declarations
float temp;

//Statements
temp = *pWalker;
*pWalker = *holder;
*holder = temp;

return;
}//exchange


*/
void outPut(float* arr, float* smallArr, float* bigArr, int num)
{
//Local Declarations
int i;

//Statements
printf(" Ascending Order Original Order Descending Order ");
printf("========================================================= ");
for(i=0; i<num; i++)
{
printf("%10.1f %10.1f %15.1f ",*(smallArr+i),*(arr+i),*(bigArr+i));
}

return;
}//outPut

Explanation / Answer

If you want smallArr and bigArr to point to float* arr, then they are pointers to an array, not an array of pointers. While both can be written as float**, it is important to understand the difference.

float* arr; // here is the starting array

float** smallArr = &arr; // create a pointer to arr and store it in smallArr

float** bigArr = &arr; // create a pointer to arr and store it in bigArr

However, since arr is itself a pointer (an array is a pointer to the first location of the array), you can also write

float* smallArr = arr;

float* bigArr = arr;

That does not make a copy of arr. smallArr and bigArr are point to the same location as arr, and any changes to them will be reflected in arr.

For example, if I wrote

float* smallArr = arr;

smallArr[1] = 20.0;

then arr[1] is also 20.0. This is true even when passing smallArr to methods, because it is a pointer. But if you want to pass smallArr to a method, create a new array, and store it in smallArr, you need to pass a pointer to smallArr.