Declare a Character array Arr of size 5. Read characters in to it without scanf. Using a pointer variable, change the characters in the Array . Print the original Arr and then the changed array with Address.
Asked
Active
Viewed 78 times
1 Answers
0
char *charArr[5]; // Declare a Character Array of Size 5 (1)
fgets(charArr, 5, stdin); // Read characters into array without scanf (2)
void change(int **array, int length){
*array = malloc(length * sizeof(int));
if (*array == NULL)
return;
for (int i = 0 ; i < length ; i++)
(*array)[i] = 1;
}
}
printf("%s\n", charArr); // simply prints array of chars (3.a) Inital array set of vals
change(&charArr, 5); // change values within array
printf("%s\n", charArr); // simply prints array of chars (3.b) array set of vals after change
free(array); // Free's up Malloc
I'm not to familiar with C but, I think it's something along those lines. I manged to extrapolate a solution using the resources below
NOTE if this does fail, please review the stackoverflow link below. It goes into describing that using malloc with predefined memory will likely throw an exception.
Resources:

Ruben Helsloot
- 12,582
- 6
- 26
- 49

TheoNeUpKID
- 763
- 7
- 11