2

I am learning how to use dynamic arrays in C. What I want to do is to create a dynamic array data, and put "1" into the first entry using the function test().

void test(void)
{

data[0] = 1;

}


int main(void)
{

int *data = malloc(4 * sizeof *data);

test();

return 0;
}

This compiles in Visual Studio 2010 but the program crashes when run. Instead of using test(), using data[0] = 1 works.

My (newbie) guess is that I need to pass a pointer to data to function test(). How should I write this?

Attempt

void test(int *data)
{

data[0] = 1;

}

Then, in main use test(data) instead of just test().

Edit

The attempt works. However, is this a "proper" way of doing it?

Legendre
  • 3,108
  • 7
  • 31
  • 46
  • 1
    Was your attempt successful ? If not what error did you get and what do you interpret from it ? – asheeshr Dec 06 '12 at 14:48
  • So far as I can see, your first attempt should never have compiled since `data` was not defined in the `test` function. Your second attempt looks like it should have worked, what was the result? – devrobf Dec 06 '12 at 14:49
  • @AshRj - The attempt worked! Is this the "proper" way of doing it though? – Legendre Dec 06 '12 at 14:53

3 Answers3

2

When you use a local variable in C, (dynamic or static, array or not), you need to pass it to the function that will be using it. That's what's wrong with your initial code, test() doesn't know anything about data.

When you declare an array, (dynamic or static) you can pass it to the function in the same ways. The following code is pretty pointless, but it illustrates that using a dynamic array is really no different than a static array.

void assign_function(int arr[], int len_of_arr, int *arr2, int len_of_arr2);
void print_function(int *arr, int len_of_arr, int arr2[], int len_of_arr2);

int main()
{
    int data[2] = {0}; // static array of 2 ints
    int *data2 = malloc(3 * sizeof(int)); // dynamic array of 3 ints

    assign_function(data, 2, data2, 3);
    print_function(data2, 3, data, 2);

    free(data2); // One difference is you have to free the memory when you're done

    return 0;
}

So we can pass the arrays, be they dynamic or static, via array[] or as a pointer, but we need to pass an int along as well so we know how big the array is.

void assign_function(int arr[], int len_of_arr, int *arr2, int len_of_arr2)
{
    int count;
    for(count = 0; count < len_of_arr; count++) //This is the static array
        arr[count] = count;

    for(count = 0; count < len_of_arr2; count++) //This is the dynamic array
        arr2[count] = count;
}

Then just for fun I reverse which array is pass in arr and arr2 here, and also how they're accessed:

void print_function(int *arr, int len_of_arr, int arr2[], int len_of_arr2)
{
    int count;
    for(count = 0; count < len_of_arr; count++)   //This is the dynamic array now
        printf("arr[%d] = %d\n", count, *(arr+count));

    for(count = 0; count < len_of_arr2; count++)  //And this is the static array
        printf("arr2[%d] = %d\n", count, *(arr2+count));
}

Point being, passing via [] or as a pointer, and accessing via [] or a deferenced pointer is up to you, both are fine, both work. I try to avoid pointers when I can as they tend to be hard to read and more error prone when writing.

Mike
  • 47,263
  • 29
  • 113
  • 177
1

You can pass arrays dynamically in two ways :

  • Using a simple pointer and then using pointer arithmetic to manipulate
void test (int * data, int i)
{
    *(data + i) = 1; //This sets data[i] = 1
}
  • Or this way :
void test(int data[], int i)
{
    data[i] = 1; //This is the more familiar notation
}

Either of these ways is the 'proper' way to go about this.

explorer
  • 944
  • 8
  • 18
asheeshr
  • 4,088
  • 6
  • 31
  • 50
  • My attempt worked. This actually doesn't. Thanks for the help in the comments. Do you want to revise your answer so I can accept it? Also, is this a "proper" way of doing it? – Legendre Dec 06 '12 at 14:57
  • @Legendre, yes it is proper way. – Anon Dec 06 '12 at 14:59
  • 1
    @Legendre Well you simply need to call the test function with `test(data);` too from your main... Oh and, good practice, *don't* forget to `free` the memory you allocated. Since your program exits afterwards in this case the OS will handle it, but its good practice to always free the resources you allocated. – Jite Dec 06 '12 at 15:00
  • If you're implying that `int i` is the number of elements in the array, then you just accessed memory outside of it by using `data[i]`. If you're implying that the `int i` is just some static number like `0` to access the array `data`.. then that's pointless and you should just hardcode it. – Mike Dec 06 '12 at 15:24
  • `i` is the index to be manipulated. I am just giving a conceptual example for the OP (i agree that it is not the best example). – asheeshr Dec 06 '12 at 15:29
0

The variable 'data' in test is locally scoped. It's not the same 'data' that is in main. You should pass a pointer to 'data' through the parameters of test().

munk
  • 12,340
  • 8
  • 51
  • 71