returning array?

This is a discussion on returning array? within the C Programming forums, part of the General Programming Boards category; how to do it? int[] func(int x); // doesn't work []int func(int x); // doesn't work int[3] func(int x); // ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    returning array?

    how to do it?

    int[] func(int x); // doesn't work

    []int func(int x); // doesn't work

    int[3] func(int x); // doesn't work

    is there any solution?

    I know that

    int* func (int x);

    but then I must use malloc and so on.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The name of an array is just a pointer to the array, cell zero. You cannot return an array any other way in C other than by pointer.

    Keep in mind that you cannot dynamicly create "arrays". You can allocate consecutive blocks of memory, which you can treat an array, but you don't actually do:

    int array[][] = new array[x][y];

    Also keep in mind that unless the array is static, there is no way to return an array other than to malloc memory for it. In which case, you just end up returning a pointer to allocated memory anyway.

    The answer to your question:

    int * myfunc( );

    Return a pointer to allocated memory or to a static array inside the function.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    You can use :
    int* function(int x)

    You do not have to use malloc. You can declare a local variable in the calling function and pass it to function. Then you do not even have to return it.

    Here is an example:
    Code:
    int* ChangeNumber(int *Num){
         int i = 0;
         for(i=0;i<10;i++){
              Num[i] = i;
         }
         return Num;
    }
    
    int main(void){
         int Numbers[10] = {0};
         ChangeNumber(Numbers);
         return 0;
    }
    You can return the int array this way, but as you can see it is not necessary to set Numbers = ChangeNumber(Numbers) because you are passing the address of the array.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    thank you for answering but I have some questions more.
    Code:
    int* ChangeNumber(int *Num){
         int i = 0;
         for(i=0;i<10;i++){
              Num[i] = i;
         }
         return Num;
    }
    
    int main(void){
         int Numbers[10] = {0};
         ChangeNumber(Numbers);
         return 0;
    }
    this code doesn't work.

    what I want to do is
    Code:
    int* funk(int a[]){
         blaa blaa blaa
         do something
         return array;
    }
    
    int main(int argc, char *argv[]){
          int array1[10], array2[10];
          array1=func(array2);
          return 0;
    }

    &#91;code]&#91;/code]tagged by Salem

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    thanks, know I know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. Returning an Array
    By mmarab in forum C Programming
    Replies: 10
    Last Post: 07-19-2007, 07:05 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21