如何在C ++中将char **转换为char *

时间:2016-01-29 18:54:24

标签: visual-c++

我很高兴如何在C ++中将char **char *相提并论。

void Text(char **Message)
{ 
 char* result = (char*)&Message; // It doesnt work :(
}

有任何线索吗?

2 个答案:

答案 0 :(得分:1)

我创建了一个程序来帮助您:

#include <iostream>

using namespace std;

void Text(char **Message, int length)
{ 
    while (length > 0) {
        char* result = *(Message++);
        length--;

        cout << result << endl;
    }
}

void main()
{
    char *names[] = {
        "John", "Mona",
        "Lisa", "Frank"
    };

    Text(names, 4);
}

要取消引用指针,只需使用*Message

答案 1 :(得分:0)

这是正确答案

int main()
{
    char  var = 'A';
    char  *ptr;
    char  **pptr;

    // take the address of var
    ptr = &var;

    // take the address of ptr using address of operator &
    pptr = &ptr;

    // take the value using pptr
    cout << "Value of var :" << var << endl;
    cout << "Value available at *ptr :" << *ptr << endl;
    cout << "Value available at **pptr :" << **pptr << endl;

    Text(pptr);

    return 0;
}

void Text(char **Message)
{
    char  *msg = *Message;
    Print(msg);
}

void Print(char * data)
{
cout << "char is :" << *data << endl; // It gives A

}