// C code showing four ways to change two different array types ... int[] and char[]: // Reference Return way // Pointer Return way // Pointer to Pointer way // Pointer Return malloc way #include #include #include int& smallestElementRef(int arr[], int arrSize); int* smallestElementPtr(int arr[], int arrSize); void smallestElementFix(int** arr, int arrSize, int putTo); int* smallestElementMallocPtr(int arr[], int arrSize, int putTo); char& csmallestElementRef(char arr[], int arrSize); char* csmallestElementPtr(int arr[], int arrSize); void csmallestElementFix(char** arr, int arrSize, int putTo); char* csmallestElementMallocPtr(char arr[], int arrSize, int putTo); int& smallestElementRef(int arr[], int arrSize) { int ret = 0; if (arrSize > 0) { for (int i=0; i arr[i]) ret = i; } } return (int&)arr[ret]; } int* smallestElementPtr(int arr[], int arrSize) { int ret = 0; if (arrSize > 0) { for (int i=0; i arr[i]) ret = i; } } return &arr[ret]; } void smallestElementFix(int **arr, int arrSize, int putTo) { int ret = 0; if (arrSize > 0) { for (int i=0; i ((*arr)+i*sizeof(int))) ret = i; } } *((*arr)+ret*sizeof(int)) = putTo; } int* smallestElementMallocPtr(int arr[], int arrSize, int putTo) { int ret = 0; int *arrout = (int *)malloc(sizeof(int) * arrSize); if (arrSize > 0) { for (int i=0; i arr[i]) ret = i; } } arrout[ret] = putTo; return arrout; } char& csmallestElementRef(char arr[], int arrSize) { int ret = 0; if (arrSize > 0) { for (int i=0; i arr[i]) ret = i; } } return (char&)arr[ret]; } char* csmallestElementPtr(char arr[], int arrSize) { int ret = 0; if (arrSize > 0) { for (int i=0; i arr[i]) ret = i; } } return &arr[ret]; } void csmallestElementFix(char **arr, int arrSize, int putTo) { int ret = 0; if (arrSize > 0) { for (int i=0; i ((*arr)+i*sizeof(char))) ret = i; } } *((*arr)+ret*sizeof(char)) = putTo; } char* csmallestElementMallocPtr(char* arr, int arrSize, int putTo) { int ret = 0; char *arrout = (char *)malloc(sizeof(char) * (arrSize+1)); if (arrSize > 0) { for (int i=0; i arr[i]) ret = i; } } arrout[ret] = putTo; return arrout; } int main (void) { int scores[7] = { 67, 87, 74, 86, 78, 77, 68 }; char cscores[8]; int* pscores = &scores[0]; char* pcscores = &cscores[0]; for (int i=0; i<7; i++) { cscores[i+1] = 0; cscores[i] = scores[i]; } for (int i=0; i<7; i++) { cout << scores[i] << " "; } cout << endl << cscores << " Initial values" << endl << endl; // Reference Return way smallestElementRef(scores, 7) = 89; smallestElementRef(scores, 7) = 65; csmallestElementRef(cscores, 7) = 89; csmallestElementRef(cscores, 7) = 65; for (int i=0; i<7; i++) { cout << scores[i] << " "; } cout << endl << cscores << " Reference Return way" << endl; cout << endl; // Pointer Return way *smallestElementPtr(scores, 7) = 73; *smallestElementPtr(scores, 7) = 69; *csmallestElementPtr(cscores, 7) = 73; *csmallestElementPtr(cscores, 7) = 69; for (int i=0; i<7; i++) { cout << scores[i] << " "; } cout << endl << cscores << " Pointer Return way" << endl; cout << endl; // Pointer to Pointer way smallestElementFix(&pscores, 7, 88); smallestElementFix(&pscores, 7, 84); csmallestElementFix(&pcscores, 7, 88); csmallestElementFix(&pcscores, 7, 84); for (int i=0; i<7; i++) { cout << scores[i] << " "; } cout << endl << cscores << " Pointer to Pointer way" << endl; cout << endl; // Pointer Return malloc way memcpy(scores, smallestElementMallocPtr(scores, 7, 89), sizeof(scores)); memcpy(scores, smallestElementMallocPtr(scores, 7, 85), sizeof(scores)); memcpy(cscores, csmallestElementMallocPtr(cscores, 7, 89), sizeof(cscores)); memcpy(cscores, csmallestElementMallocPtr(cscores, 7, 85), sizeof(cscores)); for (int i=0; i<7; i++) { cout << scores[i] << " "; } cout << endl << cscores << " Pointer Return malloc way" << endl; cout << endl; return 0; }