tags:

views:

61

answers:

3
#include <stdio.h>
void func(int arr[],int xNumOfElem)
{
    int j;
    for(j=0; j<xNumOfElem; j++)
    {
       arr[j] = j + arr[j];
       printf("%d\t",arr[j]);
    }
    printf("\n");
}

int main()
{

    int *a,k;
    a = (int*) malloc(sizeof(int)*10);


    for(k = 0; k<10; k++)
    {
        a[k] = k;
        printf("%d\t",a[k]);
    }
    printf("\n");

    func(a,10); //Func call

    free(a);                                                                                                                              
}

Inside the the function "func" who will allocate/deallocate memory for dynamic array "arr".

arr is an function argument.

A: 

One need not allocate memory for array arr in the function fun. The memory for array is allocated in the main function before the fun is called. Also main is deallocating the memory once it is done using the memory; so fun need not do deallocating as well.

codaddict
no it is pass by value if u alter it contents inside the function it wont reflect in the main function. So somehow memory is created for "arr". ???
Riyaz
Arrays are passed by address. If you make changes to `arr` inside function `fun` it will be reflected in `main`. Give it a try. This shows there is just one copy of the entire array which was allocated in `main`.
codaddict
No It wont reflect in this case!!, Print the conetents of actual array after calling function. I verified it.
Riyaz
@Riyaz: You've **not** tried it correctly. Take a look at this: http://www.ideone.com/YhMtG
codaddict
A: 

Think of it this way:

void func(int arr[],int xNumOfElem)
{
    int j;
    for(j=0; j<xNumOfElem; j++)
    {
       arr[j] = j + arr[j];
       printf("%d\t",arr[j]);
    }
    printf("\n");
}

is equivalent to:

void func(int *arr /* look here */,int xNumOfElem)
{
    int j;
    for(j=0; j<xNumOfElem; j++)
    {
       arr[j] = j + arr[j];
       printf("%d\t",arr[j]);
    }
    printf("\n");
}
BobbyShaftoe
I got this doubt because in some hdl laguage array can be passed by value or pass by reffrence. Also i made mistake in this c file by asigning a[k] = k; after calling function func(). So values are are getting changed again here. OK I will close thiss issue. Thanks!!
Riyaz
+1  A: 

You don't pass an array, you only pass the pointer to an int. The line

void func(int arr[],int xNumOfElem)

is just an other way to write

void func(int* arr,int xNumOfElem)

the compiler itself can't allocate memory for a copy of a, as the size of a is dynamic and therefor unknown to the compiler. print the values of a after the call to func to see that a has changed.

To get a copy change your func method to:

void func(int const* const b,int xNumOfElem)
{
    int* arr = malloc(sizeof(int)*xNumOfElem);
    memcpy(b,arr,xNumOfElem);
    int j;
    for(j=0; j<xNumOfElem; j++)
    {
       arr[j] = j + arr[j];
       printf("%d\t",arr[j]);
    }
    printf("\n");
    free(arr);
}
josefx
Thanks for ur feedback. I got my answer.
Riyaz