return values

This is a discussion on return values within the C++ Programming forums, part of the General Programming Boards category; So, i declare an array and then just return a pointer to the array and use a counter to display/use ...

  1. #16
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    So, i declare an array and then just return a pointer to the array and use a counter to display/use the data in the array.

    how will te code look like ? something like return *pi;??
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  2. #17
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    If you use grumpy's code.. itd be like this (and of course you could pass parameters to aFunction instead):

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct theStruct
    {
        int a,b,c,d;
    };
    
    theStruct aFunction();
    
    int main()
    {
      theStruct anObject;
      anObject = aFunction();
    
      cout << anObject.a << endl;
      cout << anObject.b << endl;
      cout << anObject.c << endl;
        
      cin.get();
    }
    
    theStruct aFunction()
    {
      theStruct theObject;
      theObject.a = 1;
      theObject.b = 2;
      theObject.c = 3;
      theObject.d = 4;
    
      return theObject;
    }
    Looking at that makes me cry.. it looks like such an inefficient method, but then again its late and I've never tried anything like that before.. I'll see in the morning if I can come up with any better ideas for passing lists, for own benefit.

    Looking at it it looks like I should return a pointer and make it = new theObject, but I cant think of how I'd recieve it.. its too late, hope it gives you something to work with.
    Last edited by Dae; 06-22-2005 at 02:59 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #18
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    I actually understand it! Thanks
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

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