Skip to main content
GameDev.net gamedev.net
🔒 Locked

Returning arrays

Started by Zeke Nov 21, 2000 at 9:36 AM 2 replies 450+ views
Original Post
Zeke
Zeke
If i have a function, say CreateArray() that creates an array but i want to return the array to use it in the part of a project that calls the function how would i store the returned array? i tried: Array[index]=CreateArray(); CreateArray() { blah blah return NewArray[index]; } and also NewArray=CreateArray; CreateArray() { blah return Array; } The only way i have been able to do this is if NewArray is a global variable and CreateArray() changes it. Is there a better way to do it? Thanks for any help "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face
Teej
Teej
      
void main()
{
int *piArray;

// Create an array with 5 elements

piArray = CreateArray(5);

// Display the array elements

for (int i = 0; i < 5; i++)
{
cout << piArray<i> << endl;
}

delete [] piArray;

}


int* CreateArray(int numElements)
{
int *piTempArray;

// Create the new array

piTempArray = new int[numElements];

// Fill it with test values

for (int i = 0; i < numElements; i++)
{
piTempArray<i> = i;
}

return piTempArray;
}


Good?

Teej (there's something flaky about using source blocks... that piArray on the cout line should have [] not <>)


Edited by - Teej on November 21, 2000 11:14:13 AM
Jumpster
Jumpster
quote:

there''s something flaky about using source blocks... that piArray on the cout line should have [] not <>




You used the letter "i" as the index of the array. The source section translates it to < "i" > (less the quotes) so it doesn''t italicize the rest of the source block.

Regards,
Jumpster
Regards,JumpsterSemper Fi
Zeke
Zeke
Thanks Teej. That has been a big help
Just my thoughts take them as you will. "People spend too much time thinking about the past, whatever else it is, its gone"-Mel Gibson, Man Without A Face

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.