警告:缺少终止“字符[默认启用]

时间:2016-05-23 14:25:10

标签: c string-literals json-c

我在下面发现这个奇怪的错误

json.c:81:19: warning: missing terminating " character [enabled by  default]
json.c:81:3: error: missing terminating " character
json.c:82:32: error: expected ‘,’ or ‘;’ before ‘:’ token
json.c:90:22: warning: missing terminating " character [enabled by default]
json.c:90:21: error: missing terminating " character

CODE:

int main()
{
  char * string = "{
                  "sender" : "joys of programming",

                   "receiver": [ "123",
                                 "345",
                                 "654",
                                 "432"
                               ]

                }";
 printf("JSON string: %sn", string);
 json_object * jobj = json_tokener_parse(string);
 json_parse(jobj);
 return 0;
 }

我理解错误大约是char * string行。但不知道如何解决它。

2 个答案:

答案 0 :(得分:2)

你必须:

  1. 转义" char,因为它是一个用于定义a的特殊字符 C-String文字。
  2. 对于多行字符串,您必须使用""为每一行定义每一行作为单个C字符串
  3. 因此,生成的代码是

      char * string = "{"
                      "\"sender\" : \"joys of programming\","
                      "\"receiver\": [ \"123\","
                                      "\"345\","
                                      "\"654\","
                                      "\"432\""
                                    "]"
                     "}";
    

答案 1 :(得分:1)

在引号内使用引号时,需要使用转义字符。

  char * string = "{ "
                  "\"sender\" : \"joys of programming\"," 

                   "\"receiver\": [ \"123\","
                                   "\"345\","
                                   "\"654\","
                                   "\"432\""
                               "]"

                "}";

这样就可以了。