1

Can I do this in C:

void myFunc(int *vp) {
    // do some stuff with vp
}

int main() {

    int v[5] = {1,2,3,4,5};
    myFunc(v);

    return 0;
}

I mean, what would be the correct? myFunc(&v); ?

Thanks!!

3
  • 1
    Are you asking because you got an error, or asking whether it works consistently? Commented Sep 26, 2013 at 4:37
  • 4
    Try it. Run it. Maybe it works. Either way, you learn something. :) Commented Sep 26, 2013 at 4:40
  • Yes! I seems to work. When I run it on VS2012 everything looks fine. But when I run it on Linux, I got segmentation fault. I don't know exactly if this is the problem. I got segmentation fault after some 4 hours of computation. I asked because I was suspecting that this might be the problem. Commented Sep 26, 2013 at 12:17

3 Answers 3

4

Arrays decay to pointers when you pass them as arguments. However, array decay is not the same as taking the address of an array.

"Decay" is how some types are transformed when passed as function arguments. Even though v's type is int [5], it becomes int* when you pass it to a function. This is a behavior a lot of people don't like, but there's nothing to do about it.

Note that, on the other hand, the type of &v is int (*)[5], that is, a pointer to an array of 5 integers. This type doesn't decay, that is, it doesn't transform automatically into another type if you pass it as a function parameter (and that's also why it wouldn't work if you used it in your example, since you need a pointer to integers, not a pointer to an array of integers).

The "correct" thing to do (assuming decay is OK) is to do myFunc(v), just as you're doing in your snippet. Keep in mind that you lose array bounds information when you do it.

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

2 Comments

Could you instead pass &v and then dereference it inside myfunc? Would this avoid the decay?
@JacKeown, there is no way to access array elements that doesn’t decay the array. In (*v)[0], (*v) is decayed before the subscript happens.
1

Yes ... Your code is correct.

Here v==&v[0] array name is equal to address of first element of array

    myFunc(v); 

passing array name as argument that means you are passing address of first element in array.

    void myFunc(int *vp)  

Here you are using pointer. which store the address of first element of array which is passed so you can access the block which is covered with the array.by incrementing the pointer location.

And

    myFunc(&v);   

    &v==&&v[0];

&v is address of address of array first element.

Now

  void myFunc(int *vp)      

Here You got address of address of array first element, This is not pointing to array. Instead pointing some memory location.Now You can't access the array by incrementing the pointer.

Comments

1

Your code is correct It will work.... But you should take extra care to check the boundary condition. Please look through the code.

void myFunc(int *vp) {
    vp[5] = 30;
}

int main() {

    int v[5] = {1,2,3,4,5};
    int a = 10;
    printf("Value of a before fun call %d\n", a);
    myFunc(v);
    printf("Value of a before fun call %d\n", a);
    return 0;
}

similarly

void myFunc(int *vp) {
    vp[5] = 30;
    myFunc2(vp);
}

void myFunc2(int *vp) {
    vp[6] = 30;
}

int main() {

    int v[5] = {1,2,3,4,5};
    int a = 10;
    printf("Value of a before fun call %d\n", a);
    myFunc(v);
    printf("Value of a before fun call %d\n", a);
    return 0;
}

This will result in segmentation fault due to stack curruption. Since local variables are in stack.

Comments

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.