1 stupid question

This is a discussion on 1 stupid question within the C++ Programming forums, part of the General Programming Boards category; how do I return an array?...

  1. #1
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    857

    1 stupid question

    how do I return an array?
    Yoshi

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    If it's a local variable, you don't. If it's dynamic memory or static memory, you can just pass a pointer to it. Three examples:

    Code:
    char* funct()
    {
        char array[SOME_MAX_SIZE];
        strcpy(array,"My Buggy message");
    
         /* This is a bug. Don't do this in your code */
        /* the storage for array does not stay in scope, so you
            can't return it */
        return array;
    }
    
    static char array[SOME_MAX_SIZE];
    char* funct1()
    {
        strcpy(array, "But this is okay");
        return array;
    }
    
    /* Or... */
    
    char* funct2()
    {
        char* array = (char*)malloc(SOME_MAX_SIZE);
        strcpy(array,"This is also okay");
        return array;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >how do I return an array?
    Put it inside a struct, and return that. Personally I'd try not to use this method in any of my real-life designs though.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  2. Replies: 7
    Last Post: 11-03-2005, 11:17 PM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 07:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM

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