invalid conversion from 'char' to 'char*'

Dear guru,

The simplified code below works correctly.
But when I try to replace line 43 with out2[i] = sentence[i];, it gives this compilation error:
invalid conversion from 'char' to 'char*'
Why and how to make it work?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;


typedef std::vector <void *> gr_vector_void_star;
gr_vector_void_star  output_items;

const char* sentence("Angel");
unsigned int 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.

How to do it?
Please advise, thanks.
activecat
Last edited on
sentence is an array of chars, so sentence[i] has type char.

out2 is an array of pointers to char, so out2[i] has type char*.

So it should now be obvious why you're getting that message.

If each element of out2 is intended to be a single character, then why not make it an array of chars?
closed account (z05DSL3A)
const char* sentence : This is a pointer to a constant char
char* *out2 : This is a pointer to a pointer to a char

They are not arrays.
@ MikeyBoy,

Thanks. I forget to mention that the requirement is to correctly cast out2 from output_items[1], which is a void pointers.
Last edited on
@ 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; }


Expected output:
A
n
g
e
l
Last edited on
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.
1
2
typedef std::vector <void *> gr_vector_void_star;
gr_vector_void_star  output_items;
Last edited on
Dear all above,

I have create another thread, putting this question into a simpler way.
Refer http://www.cplusplus.com/forum/general/121976/

Let's meet there, thank a lot.
Last edited on
Topic archived. No new replies allowed.