0

I am new to C development and I am having an issue setting an arrays values in a function and returning to the calling method. The function itself has to return an int and the array size needs to be dynamic, so I am trying to update the original array utilising a pointer to the array. My code is as below:

int getArray(TestType *testArray)
{

    testArray = malloc(2 * sizeof(TestType));
    testArray[0].id = 1;
    testArray[0].testFloat = 1.5;
    testArray[1].id = 2;
    testArray[1].testFloat = 2.5;

    printf("getArray element 2 id = %d\n", testArray[1].id);

    return 1;
}

void main()
{
   TestType *testArray; 
   int i = getArray(*&testArray);   
   printf("main element 2 id = %d\n", testArray[1].id); 
}

When I run this I get the following results:

getArray element 2 id = 2
main element 2 id = 0

I have looked elsewhere and although c returning an array from a function describes a similar issue this is dealing with a char array, whilst I have a user defined struct, so don't believe I can apply the same solution.

15
  • 1
    C11 draft standard n1570: 6.5.2.2 Function calls 4 An argument may be an expression of any complete object type. In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument. 93) A function may change the values of its parameters, but these changes cannot affect the values of the arguments. Commented Aug 10, 2016 at 14:19
  • 1
    @EOF absolutely right, just my two cents, for someone who says I am new to C development, only those are pretty heavy words. :) Commented Aug 10, 2016 at 14:25
  • 1
    @SouravGhosh: I find that there really is no alternative to reading the standard if you want to actually understand what is going on in C. Much of the standard is quite readable as well. Commented Aug 10, 2016 at 14:27
  • @EOF I certainly don't completely disagree, but IMHO, to start with reading the standard, is too much to ask for. :) Commented Aug 10, 2016 at 14:28
  • 1
    @SouravGhosh: Which is why I like to cite relevant parts of the standard. That way, interested people can quickly find the section and find an entry into the subject. It's how I got into it as well. Commented Aug 10, 2016 at 14:29

1 Answer 1

2

In your code, testArray itself is being passed-by-value. Any changes made to that inside the function, will not reflect back to the caller. You need to pass the address of the testArray, i.e., use a pointer to pointer.

In this condition,

  printf("main element 2 id = %d\n", testArray[1].id); 

in main() is basically accessing invalid memory which invokes undefined behavior.

You can, however, do something like

int getArray(TestType **testArray)
{

    *testArray = malloc(2 * sizeof(TestType));
    (*testArray[0]).id = 1;  
    //....
    //....

and

 int i = getArray(&testArray);

to get the desired behaviour.

That said, just to mention getArray(*&testArray); is the same as getArray(testArray);

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

3 Comments

You can't pass arrays by value to functions (and there is no pass-by reference in C).
Many thanks for your help. I've just tried this exactly as you suggested and am still getting 0 as the output in main(). Do I need to change testArray[1].id to different syntax?
Actually the suggested code does return the first element of the array correctly, testArray[0].id. It's the second value only which is 0, testArray[1].id.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.