DREAM OF TECHNICAL ACHIEVEMENT

0%

warning: ISO C++11 does not allow conversion from string literal to 'char *'

c++ 写了一个函数:

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
#include <iostream>

using namespace std;

typedef void(TIPS)(void);

struct tip
{
char from[64];
char to[64];

TIPS *tp;
};

struct tip* create_tip(char *from,char *to, TIPS *tp){
struct tip *temp = (struct tip*)malloc(sizeof(struct tip));
if(temp == NULL){
return NULL;
}
strcpy(temp->from, from);
strcpy(temp->to, to);
temp->tp = tp;

return temp;
}

编译的时候会报如下错误:
warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
解决办法:

1
struct tip* create_tip(const char *from,const char *to, TIPS *tp)

原因是:C++11以后的标准应该不允许这样的格式.char *from是形参传过来的from是字面常量,用一个指针from去指向它,事实上没有办法通过from去改变字面常量的。