Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing pointers/arrays

Status
Not open for further replies.

jwdcfdeveloper

Programmer
Mar 20, 2001
170
US
Is there a way to passing arrays/pointers from a created function to a main function? I want to pass an array of float values, however every example I've seen only passes a single value not necessarily an array of values. Any ideas?
 
there are many ways
int* xxx()
{
return new int[100];
}
void xxx(int *& _x)
{
_x = new int[100];
}
void xxx(int ** _x)
{
*_x = new int[100];
}

int main()
{
int* x;
x = xxx();//first xxx function or
delete[] xxx;
xxx(x);//second or
delete[] xxx;
xxx(&x);//third
delete[] xxx;
return 0;
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Let's assume the following:
typedef
{
float Floats[1000];
int Used;
} T_MY_ARRAY;

float Some_Function(T_MY_ARRAY * Data)
{
float Result;
int Counter;
// Add the values together
for(Counter = 0, Result = 0.0;Counter < Data->Used;Counter++)
{
Result += Data->Floats[Counter];
}
return(result);
}

Now, by declaring
T_MY_ARRAY The_Block;
you creats The_Block from the heap, this can be OK for smaller memory chunks but should be avoided for large chunks.
To access the content you use The_Block.Floats[x] and The_Block.Used.
To call Some_Function do like this:
x = Some_Function(&The_Block);

Then You can declare
T_MY_ARRAY * The_Block;
which just means that you has declared a pointer to a block of that type/size, currently pointing to nothing.
Now you has to make it point on something:
The_Block = new T_MY_ARRAY;
At this point, if The_Block == null you has an error, no memory could be assigned.
If everything goes OK you access the content by using The_Block->Floats[x] and The_Block->Used.
To call Some_Function do it like this:
x = Some_Function(The_Block);
Remember to free The_Block when you're finished with it:
delete The_Block; // Frees the memory taken
The_Block = null; // Catches unauthorized uses after deletion, just a good idea

Totte
 
Hey IonFlipinski,

A couple of your examples are kinda along the lines of what I need, but, I am passing values from a flat file into the array, and want to return the array to a main funcion so that I can plug the values into a local variable. Totte thanks for the assist, but the concepts your using are way more than I know and need at this point. I am a C++ newbie.
 
you could do something like this:
Code:
#include<fstream.h>
......
int main()
{
   ifstream xxx(&quot;yourfile&quot;);
   int i = 0;
   float float_value[.....];
   while(xxx){xxx >>float_value[i];i++;if(.....)break;}
   do something....
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top