Can someone give me an example of this too? Sorry, but i din't know if to open a new thread or not since its a different kind of question?
sorry for the confusion, i edited it.
Printable View
Can someone give me an example of this too? Sorry, but i din't know if to open a new thread or not since its a different kind of question?
sorry for the confusion, i edited it.
What do you mean by:
I don't get it?Quote:
range of values stored into a integer
i don't either but this would return a range :
Code:#include <iostream.h>
int main()
{
for (int x = 1; x <=100; x++)
{
cout<<x<<endl;
}
return 0;
}
You can only store 1 value in an integer, though you could use bitshifting and masking to store more:
If you really want to return a range, you could use a structure:Code:int SetValues(short First, short Last)
{
int Var;
Var = First;
Var |= (Last << 16);
return Var;
}
short GetFirst(int Var)
{
return Var & 0x00FF;
}
short GetLast(int Var)
{
return Var & 0xFF00;
}
Code:typedef struct
{
int First;
int Last;
}RANGE;
RANGE GetRangeFunction()
{
RANGE Range;
Range.First = 12;
Range.Last = 36;
return Range;
}
> Can you return a range of values stored into an array of integer?
An array of integers is a range of values. Just return a pointer to the array:
-PreludeCode:#include <iostream>
int *f()
{
int *range = new int[5];
for ( int i = 0; i < 5; ++i )
range[i] = i;
return range;
}
int main()
{
int *r = f();
for ( int i = 0; i < 5; ++i )
std::cout<< r[i] <<std::endl;
delete[] r;
std::cin.get();
}
Thanks prelude, that really helped me understand.
:D