Passing Variable Length Arrays to a function

Posted by David Bella on Stack Overflow See other posts from Stack Overflow or by David Bella
Published on 2010-05-04T03:54:49Z Indexed on 2010/05/04 4:08 UTC
Read the original article Hit count: 343

Filed under:
|
|
|

I have a variable length array that I am trying to pass into a function. The function will shift the first value off and return it, and move the remaining values over to fill in the missing spot, putting, let's say, a -1 in the newly opened spot.

I have no problem passing an array declared like so:

int framelist[128];
shift(framelist);

However, I would like to be able to use a VLA declared in this manner:

int *framelist;
framelist = malloc(size * sizeof(int));
shift(framelist);

I can populate the arrays the same way outside the function call without issue, but as soon as I pass them into the shift function, the one declared in the first case works fine, but the one in the second case immediately gives a segmentation fault.

Here is the code for the queue function, which doesn't do anything except try to grab the value from the first part of the array...

int shift(int array[])
{
    int value = array[0];
    return value;
}

Any ideas why it won't accept the VLA?

I'm still new to C, so if I am doing something fundamentally wrong, let me know.

© Stack Overflow or respective owner

Related posts about c

    Related posts about arrays