#include <iostream>
#include <cstring>
#include <vector>
usingnamespace std;
typedef std::vector <void *> gr_vector_void_star;
gr_vector_void_star output_items;
constchar* sentence("Angel");
unsignedint counter(0);
int noutput_items = 5;
int main()
{
cout << "sentence is '" << sentence << "'" << endl;
// initialize output_items[0]
float *temp1[ noutput_items ];
output_items.push_back( temp1 );
// store values into output_items[0]
float *out1 = (float *) output_items[0];
for (int i=0; i < noutput_items; i++)
{ out1[i] = i * 1.1; }
// retrieve values from output_items[0]
float *display1 = (float *) output_items[0];
for (int i=0; i < noutput_items; i++)
{ cout << display1[i] << endl; }
cout << endl;
// initialize output_items[1]
char* *temp2[ noutput_items ];
output_items.push_back( temp2 );
// store values into output_items[1]
char* *out2 = (char**) output_items[1];
for (int i=0; i < noutput_items; i++)
{
out2[i] = "k"; // this works!
// out2[i] = sentence[i]; // error: invalid conversion from 'char' to 'char*' [-fpermissive]
// out2[i] = (char) sentence[i]; // error: invalid conversion from 'char' to 'char*' [-fpermissive]
// out2[i] = (char*) sentence[i]; // error: segmentation fault
}
// retrieve values from output_items[1]
char* *display2 = (char**) output_items[1];
for (int i=0; i < noutput_items; i++)
{ cout << display2[i] << endl; }
return 0;
}
Output of the above code:
sentence is 'Angel'
0
1.1
2.2
3.3
4.4
k
k
k
k
k
Process returned 0 (0x0) execution time : 0.002 s
Press ENTER to continue.
Note:
The objective is to be able to retrieve the characters out from output_items[1] later, as below.
1 2 3
anytype display2 = (anytype) output_items[1];
for (int i=0; i < noutput_items; i++)
{ cout << display2[i] << endl; }
Desirable output:
A
n
g
e
l
The requirement is that, output_items must be defined as a vector of void pointers. temp2, out2, sentence and display2 may be declared differently as needed.
@ Canis lupus:
I think you are right, both sentence and out2 are not arrays.
In this case how to define temp2, out2, sentence and display2 accordingly to make the program works ?
Note:
The objective is to be able to retrieve the characters out from output_items[1] later, for example, as follows.
1 2 3
anytype display2 = (anytype) output_items[1];
for (int i=0; i < noutput_items; i++)
{ cout << display2[i] << endl; }
So let me get this right - output_items is an array of pointers. What do those pointers point to? Single characters? The first character each of several character arrays?
@MikeyBoy: output_items is a vector of void pointers. Hence both output_items[0] and output_items[1] are void pointers. output_items[0] points to an array of float numbers. output_items[1] can point to anything as long as our objective is achieved.