Arrays.. oh the pain...

This is a discussion on Arrays.. oh the pain... within the Windows Programming forums, part of the Platform Specific Boards category; I would post this on the C++ board, but I've been having a problem with it (any board operators out ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    Arrays.. oh the pain...

    I would post this on the C++ board, but I've been having a problem with it (any board operators out there?). Whenever I post something on that board, everything's fine. When I try to go back on, though, even immidiately, it's gone. No, it didn't move down the board, it's just gone. Can I just not see them or are they gone?

    ANYWAY.....................

    How can you make a function return an array (of floats)?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856

    Not exactly what you asked for but

    double dArray[MAX_ARRAY];

    prototype
    MyFunct(double **pdArray);

    call
    MyFunct(&dArray);

    in MyFunct() lock the array back to a pointer of the same size with a type cast

    double *pLocalArray;

    pLocalArray=(double*)pdArray;

    now use the array (with pLocalArray[Index]) and on return the values will be modified by actions in MyFunct()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125
    Might not be any different but you could try

    float abc[20];
    return abc *;

    I think so at least if it dosen't work I'm so sorry...
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    You see, I'm trying to assign an array with a function. The only problem is- it doesn't work. How can I do it? Functions are doomed to only return one value?

    float yourmom[20] = NewMom(value,value,value,etc.)

    NewMom(variable value, variable value,etc.)
    {
    if (statement == TRUE)
    {
    float array[20] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    }
    else
    {
    float array[20] = NULL;
    }

    return array;
    }

    Is there a way I could encript a package of info in one variable to return, and then open it to an array later?
    Last edited by CodeMonkey; 11-29-2001 at 03:12 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Functions are doomed to only return one value?
    ..but as novacain has already pointed out, you can pass as many parameters as you like -

    Code:
    void fn(bool someval,float* arr)
    {
    
    	for(int i=0;i<20;i++)
    		if(someval==true)
    			arr[i]=1;
    		else
    			arr[i]=0;
    }
    
    int main()
    {
    	float yourmom[20];
    	fn(true,yourmom);
    
    	return 0;
    }
    zen

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Yes, I will do that. But the question still remains...

    "Can a function return an array of values?"
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It can return a pointer to an array, but you'd have to ensure that the memory that contains the array is still valid after the function has returned - you wouldn't be able to create the array locally on the stack in the function.
    zen

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Thankx.

    So if I create a function that returns a pointer to an array that will not be destroyed, this will work?:

    float YourMom[20] = NewMom();
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    No, as you can only return a pointer you'll have to assign the return value to a pointer -

    Code:
    float* fn()
    {
    	float* ptr = new float[20];
    	return ptr;
    }
    
    
    
    int main()
    {
    	float* YourMum= fn();
    	//do stuff
    	delete []YourMum;
    	return 0;
    }

    Alternatively you could wrap an array in a struct and return an instance of the struct.
    zen

  10. #10
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    THANK YOU! Finally, an answer I can DO something with!
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  11. #11
    Sayeh
    Guest
    Of course you can return the address of an array. The only killer is that the array must be allocated globally (or locally to the file), otherwise, as soon as the function returns, the stackspace where the array was allocated is considered "disposed of" and is dangerously volatile.

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    5
    Yes, opening an undelclared address for writing is how many viruses work. Write 0's everywhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. These string arrays are a pain!
    By FreeCare in forum C Programming
    Replies: 9
    Last Post: 10-26-2008, 03:27 PM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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