我正在使用数组和指针进行程序,但出现此分段错误。谁能解释为什么我在这段代码中遇到这种分段错误。
我的代码:
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
int main(void)
{
CURL *curl;
CURLcode res;
struct curl_slist *headers = NULL;
char CPID[50] = "98f5b52a59aa4503999894c10bc33dca" ;
char Uni_ID[10] = "Demo123" ;
char *postData;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl,CURLOPT_URL,"https://avnetagent.iotconnect.io/api/2.0/agent/sync");
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
//postData = "{\"cpId\":\""+CPID+"\",\"uniqueId\":\""+Uni_ID+"\",\"option\":{\"attribute\":false,\"setting\":false,\"protocol\":false,\"device\":false,\"sdkConfig\":false,\"rule\":false}}";
postData = "{\"cpId\":\"";
strcat(postData,CPID);
strcat(postData,"\",\"uniqueId\":\"");
strcat(postData,Uni_ID);
strcat(postData,\"rule\":false}}");
headers = curl_slist_append(headers,"accept: application/json");
headers = curl_slist_append(headers,"Content-Type: application/json");
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,postData);
/* Perform the request,res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr,"curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}