Thread: Returning arrays from a function.

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    Returning arrays from a function.

    I've searched through the site and the forums, but I can't seem to find a clear answer. Can it be done? If so, what would be an example definition. If not, is there anything else that could be done, aside from using a global variable to get the same result?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Can it be done?

    No.[*]

    >If not, is there anything else that could be done, aside from using a global variable to get the same result?

    One usual way is to pass a pointer to the start of the array as a parameter, much like strcpy.



    [*]Insert long discussion that offers various tricks and such that are almost but not quite returning an actual array.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Kinda thought so. I was already playing with the idea of passing a pointer but I hadn't quite figured out how to do it. I think I've got an idea now, though. Let me lay it out.

    Code:
    //Example code for dealing with arrays.
    #include <stdio.h>
    
    int index;
    int entry[5];
    entry[0] = 0;
    entry[1] = 1;
    entry[2] = 2;
    entry[3] = 3;
    entry[4] = 4;
    int *entryptr = &entry;
    int ansr[5];
    
    /*return pointer*/ evaluate(int *ptr)
     {
      int answer[5];
      int *answerptr = &answer;
      for(index = 0; index <= 4; ++index)
        answer[index] = ptr[index];
      return answerptr;
     }
    
    main()
     {
      ansr = evaluate(entryptr);
      printf("%i%i%i%i%i", ansr[0], ansr[1], ansr[2], ansr[3], ansr[4]);
     }
    Would that give the output of "12345"?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Would that give the output of "12345"?
    No.

    >int *entryptr = &entry;
    The name of an array is almost always converted to a pointer to the first element, so the address-of operator is not needed.

    >/*return pointer*/ evaluate(int *ptr)
    If only the compiler understood comments:
    Code:
    int *evaluate ( int *ptr )
    >return answerptr;
    answerptr points to a local array, which will be destroyed when the function returns. The memory will no longer belong to you and likely contain garbage.

    >ansr = evaluate(entryptr);
    ansr is not a pointer, it's an array, and arrays cannot be assigned.
    My best code is written with the delete key.

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Mm-kay. I get all that (figured I made a bunch of stupid mistakes). What I can't figure out is how to return the answer without using a global variable. Here, let me give you a claraer idea of what I'm actually trying to do.

    I'm trying to write two functions. InitIntArray and InitCharArray. They're going into my personal header file so I can use them in any program I write. They need to take three arguments: a pointer to the array to be initialized, the number of elements in the array, and what each element should be initialized to. Any ideas? Cause I'm out.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    void InitIntArray(int *array, int size, int value)
    {
       while ( size-- )
       {
          *array++ = value;
       }
    }
    For the character version, just use memset.
    Last edited by Dave_Sinkula; 03-21-2005 at 02:43 PM. Reason: Added link.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Wow. Thanks for helping, but I must be more of a newbie than I thought, cause I really don't get it.

    EDIT: Nevermind. I plugged it into my compiler and messed with it for a bit. It was just the -- and the ++ that I'm not used to using in those instances. It makes perfect sense, now. Thanks a lot. ^_^ I'm happy now.
    Last edited by Jaken Veina; 03-21-2005 at 04:29 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Is this easier?
    Code:
    #include <stdio.h>
    
    void InitIntArray(int *array, int size, int value)
    {
       int i;
       for ( i = 0; i < size; ++i )
       {
          array[i] = value;
       }
    }
    
    int main(void)
    {
       int i, array[5] = {0};
       puts("Nothing up my sleeve...");
       for ( i = 0; i < 5; ++i )
       {
          printf("array[%d] = %d\n", i, array[i]);
       }
       InitIntArray(array, 5, 42);
       puts("Presto!");
       for ( i = 0; i < 5; ++i )
       {
          printf("array[%d] = %d\n", i, array[i]);
       }
       return 0;
    }
    
    /* my output
    Nothing up my sleeve...
    array[0] = 0
    array[1] = 0
    array[2] = 0
    array[3] = 0
    array[4] = 0
    Presto!
    array[0] = 42
    array[1] = 42
    array[2] = 42
    array[3] = 42
    array[4] = 42
    */
    Last edited by Dave_Sinkula; 03-21-2005 at 04:27 PM. Reason: Added color.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Bah, pooie. My edit was too slow. I got it after all.

    Just one more question. While I was searching earlier, I read about a delete operator being used on a pointer. Is it only for pointers or can it be used on anything?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Jaken Veina
    Just one more question. While I was searching earlier, I read about a delete operator being used on a pointer. Is it only for pointers or can it be used on anything?
    In C++ delete is used to free memory that was allocated using new, which is pointed to by some pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Right, I forgot about that part. So, once you declare a variable, there is no way to "re-declare" it or "un-declare" it, inside its function.

  12. #12
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    why would you want to do that?
    Registered Linux User #380033. Be counted: http://counter.li.org

  13. #13
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, in the program I'm working on, I need to be able to scan and read character strings of any size. To do that, I'm going to make a function to declare a local array of ten elements and scan the input into that. If the input is larger than ten characters, it will create more ten element arrays as it needs them. What it will do once it reads a \n, is count how many characters there are and create an array with just that many elements and copy the input into that array. But, since I have my entire main function looped indefinately so that the program will run again after it evaluates the entry, I need a way to get rid of the old array so that a new one can be declared. If it can't be done, I have an idea that should work, but this will just be easier.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    realloc is your friend, so long as you use it correctly.

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

  15. #15
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Ahh. Gotcha. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM