1

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]); 
}
7
  • 2
    you are not changing the array pointer in your main function. Commented Sep 10, 2018 at 14:01
  • @Osiris but i pass the pointer to addNewValue function, and i add new element to the array, so i think it should change the array pointer in main too? Commented Sep 10, 2018 at 14:04
  • 2
    No the pointer is passed by value, like everything in C. You change the copy of the pointer which was created at the function call. Commented Sep 10, 2018 at 14:05
  • 1
    @ThuanNguyen Yes, or you return the new array pointer. You also do not need to create a pointer you can just call the function with &var as argument. Commented Sep 10, 2018 at 14:11
  • 1
    It is almost always confusing to use the same name for a parameter within a function and in the calling code. The array inside printfArray is a copy of the array in main, and the array inside addnewValue is a different copy. If you get in the habit of using different names, it can help you avoid the mistake of thinking you're looking at the same object. Commented Sep 10, 2018 at 14:14

2 Answers 2

2

Whatever your function addNewValue(array, value, lengthP) does, it will never change the value the pointer array passed to it. Note that array in main and array in addNewValue are two different variables; the value of the former (i.e. a "memory address") is copied into the value of the latter, and addNewValue is changing a copy, not the original pointer value from main. Hence, array will always be NULL. Hence, you'll dereference NULL and yield undefined behaviour.

Change

void addNewValue(int* array, int value, int* lengthP) 

to

void addNewValue(int** array, int value, int* lengthP) 

and call it using

addNewValue(&array, value, lengthP) 

Adapt the logic in addNewValue accordingly.

Sign up to request clarification or add additional context in comments.

Comments

2

Because you don't modify the array of main (and that allocated in addNewValue is leaked BTW).

int main() {
    int *array = NULL;
    int value = 42;
    int length = 0;
    int *lengthP = &length;
    addNewValue(&array, value, lengthP);
    printArray(array, lengthP);
}

void addNewValue(int** array, int value, int* lengthP) {   
    *lengthP = *lengthP + 1;
    int length = *lengthP;

    *array = realloc(*array, length * sizeof(int));

    if(*array == NULL) { /* ... */ }

    (*array)[length - 1] = value;
}

4 Comments

Or you could pass the pointer by reference, int *&array
@Martin Cook: is this possible in C?
@StephanLechner No it is not.
Apologies, yes, that applies to c++

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.