2

I was trying to return an array from child_prog() to main(). The code looks like this:

#include<stdio.h>
#include<stdlib.h>
int* child_prog(int some_input);

void main(void){
  int C[10];
  int some_input;
  C = child_prog(some_input);
}

int* child_prog(int some_input){
  static int out[10];
  // ...
  // ... some wizardry
  return out;
}

Now the compiler generates an error saying that it can not assign to C (which is an int[] type) the value returned from child_prog (which is an int* type). Indeed, the program works fine when I make C an int* and malloc it 10 ints of memory. But I don't understand why the compiler can't assign to C (an array defined as C[10] and hence a pointer) the value returned from child_prog (an array defined as static int out[10] and hence again a pointer).

4 Answers 4

4
  1. You cannot assign to an array. You need to memcpy it.
  2. int* != int[] while the first is a pointer that might point to an int of a sequence of ints, the second is a sequence of ints
  3. You can use int *C; and pass the length of the array (if unknown at compile time) as an out parameter.
  4. static int out[10]; is not a malloced, but static.
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, understood point number-1 that int*!=int[]. But, in the case when I define C as int* C instead of int C[], and malloc it the known amount of memory, and child_prog returns to C the pointer to the static int array, shouldn't I be able to bypass the need to do memcpy?
If you are not assigning to an array, but to a pointer, you don't need memcpy.
Thanks for slaying a lot of my dragons ... rather quickly
1

One solution can be to declare C as:

int *C;

as said by Binyamin You cannot change the address of an array as it is statically allocated, which precisely you are trying to do with:

C = child_prog(some_input);

Comments

0
  • The type of int [] is int * const which means the memory it points to is a constant and trying to change it will give a compiler error. Obviously it isn't equal to int *.
  • The variable out in child_prog() is statically allocated which means its not on the stack but somewhere in the global data section. Hence no matter how many times you call child_prog(), you'll always be returned the same memory location.
  • Hence to copy the array, do a memcpy(dest,src,bytes) if you want to save the data returned from child_prog().

Comments

0
#include<stdio.h>
#include<stdlib.h>

#define SUCCESS    0 
#define FAILURE    1 

int child_prog(int some_input, int *output, int output_size);

void main(void){
  int C[10];
  int some_input;
  C = child_prog(some_input, c, 10);
}

int  child_prog(int some_input, int *output, int output_size)
{
  static int source[10];
      ...
      .../*some wizardry*/

  memcpy(output, source, output_size);


     ... if (erronous Condition)
             return FAILURE;
         else

  return SUCCESS;
}

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.