I have the following code, I pass the same pointer to both functions but I can only print out array elements in addNewValue
function.
I do the same for printArray
but the program stop and does not print anything.
What should I do to fix this problem, I have searched on stackoverflow but it seems like I can't find the answer for my problem.
int main()
{
int *array = NULL;
int value;
int length = 0;
int *lengthP = &length;
printf("Enter new value");
scanf("%d", &value);
addNewValue(array, value, lengthP);
printArray(array, lengthP);
return 0;
}
void printArray(int* array, int* lengthP)
{
int i = 0;
int length = *lengthP;
for(i = 0; i < length; i++)
{
printf("%d\n", *array[i]);
}
}
void addNewValue(int* array, int value, int* lengthP)
{
*lengthP = *lengthP + 1;
int length = *lengthP;
array = realloc(array, length * sizeof(int));
if(array == NULL)
{
printf("Error");
return;
}
array[length - 1] = value;
printf("%d", array[0]);
}