Really really easy, dumb question

This is a discussion on Really really easy, dumb question within the C++ Programming forums, part of the General Programming Boards category; This is my simple little function Code: int reverse_list(void) //creates a list of ints from 0 to N { int ...

  1. #1
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21

    Really really easy, dumb question

    This is my simple little function

    Code:
    int reverse_list(void)                //creates a list of ints from 0 to N
    {
        int N = 20;
        int temp_array[N];
        
        for(int i = 0; i < N; i++)
        {
                temp_array[N - i] = i;
        }
        
        return temp_array;
    }
    How do I return the array? It says invalid conversion from *int to int. I feel really dumb for not knowing this.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Generally, you don't.

    Pass in the array you want to fill in, and everything will work better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    Ok that makes sense.

    But...

    Quote Originally Posted by matsp View Post
    Generally, you don't.
    Does that mean that occasionally you can?

  4. #4
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    It's difficult and messy to do so. Instead you need to use pointers. Now hold on, it's not as bad as it seems. Here, lemme show you:
    Code:
    void reverse_list(int *temp)                /*creates a list of ints from 0 to N*/
    { /* Incidentally (int temp[]) should work just the same.
    
        int N = 20;
        int temp[N];
        
        for(int i = 0; i < N; i++)
        {
                temp[N - i] = i;
        }
       /* no need to return because you're manipulating the array directly through pointers.
    }
    Then to use it:
    Code:
    int array[20];
    
    /* fill the array and then */
    reverse_list (array);
    Now, that fixes the code, but there's still a big problem. It won't work. i mean it'll pass the array and whatnot, but it won't reverse the array. Instead it will fill the array with the numbers 20 to 1 regardless of what was in there. However, I leave that to you to fix.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Hexadakota View Post
    Does that mean that occasionally you can?
    Yes, there are some special cases where you can return a pointer or reference to an array (or an actual array, but that's extremely rare indeed). Just make sure that the pointer or reference you return points to data that is exists beyond the return from the function:
    1. static local variable.
    2. global variable.
    3. passed in parameter.
    4. memory allocated with new/malloc.

    In case 4, you must also remember to free the memory afterwards (using delete or free respectively).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dumb question
    By dragon2309 in forum C Programming
    Replies: 18
    Last Post: 10-29-2005, 04:27 PM
  2. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 11:43 PM
  3. Quick Easy Question
    By St0rmTroop3er in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 02-24-2004, 01:08 AM
  4. easy question about machine code
    By Jaguar in forum Tech Board
    Replies: 3
    Last Post: 10-07-2003, 10:11 AM

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