2

I just have a basic question concerning arrays in functions. I am trying to change an array in a function without returning it. I know how to do this for integers or doubles but i didn't know how to do this for arrays. So i experimented a little bit and now I am confused.

I have 2 variations of my code which i thought should do the same thing , but they don't. I pass the array b to the function Test. In the function I try to fill the array with the values 0, 1 ,2

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

void Test(int * vector){
    vector = malloc(3*sizeof(int));
    int i;
    for(i=0;i<3;i++){
        *(vector+i)=i;
    }

}

int main(){
    int b[3];
    Test(b);
    printf("%i\n",b[0]);
    printf("%i\n",b[1]);
    printf("%i\n",b[2]);
    return EXIT_SUCCESS;
}

This Version doesnt work, i don't get the expected result 0,1,2 This Code on the other hand does seem to work:

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

void Test(int * vector){
    int * b = malloc(3*sizeof(int));
    int i;
    for(i=0;i<3;i++){
    *(b+i)=i;
    *(vector+i) = *(b+i);
    }


}

int main(){
    int b[3];
    Test(b);
    printf("%i, ",b[0]);
    printf("%i, ",b[1]);
    printf("%i ",b[2]);
    return EXIT_SUCCESS;
}

Can somebody explain to me why only the second one works?

Best Regards, Rob

2
  • for (int i=0;i<3;i++) vector[i]=i; is the correct code. You've allocated memory for the array on the stack in main. So all you need to do is fill in the values. Commented Oct 7, 2016 at 15:45
  • 1
    vector = malloc(3*sizeof(int)); overwrites the vector parameter with a pointer to a new, unrelated block of memory. If you want to fill the memory pointed to by the original vector, don’t throw away that pointer. =) Commented Oct 7, 2016 at 15:47

2 Answers 2

2

When you pass an array to a function, it decays into a pointer to the first element. That's what the function sees. But then you take the function parameter vector and overwrite it with dynamically allocated memory. Then you don't have access to the array you passed in. Additionally, you have a memory leak because you didn't free the allocated memory.

In the case of the second function you don't modify vector, so when you dereference the pointer you're changing b in main.

Also, instead of this:

*(vector+i)

Use this:

vector[i]

It's much clearer to the reader what it means.

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

1 Comment

@Rob Glad I could help. Feel free to accept this answer if you found it useful.
2

test doesn't need to call malloc(). When you use an array as a function argument, it passes a pointer to the array. So you can simply write into vector[i] and it will modify the caller's array.

void Test(int * vector){
    int i;
    for(i=0;i<3;i++){
        *(vector+i)=i;
    }
}

1 Comment

Thank you very much. I did it a lot more complicated than it is I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.