Below is my source code, I am trying to store sensor information on linked list. Sensor information is received by type of JSON. And, I confirmed that it is well parsed.
However, when i am trying to store received value, then "Segmentation fault " error is occurred. I think it is because of usage of double pointer of struct. How can i solve this?
//sensor
if (!strcmp(key, "sensor") && (num_sen > 0))
{
#if DEBUG
fprintf(stderr, "\n********************SENSORINFO********************\n");
#endif
SENSOR_CONFIG **p_sen = NULL;
p_sen = mcfg->sensor_cfg;
SENSOR_CONFIG ** firstnode = NULL;//첫번째 노드
SENSOR_CONFIG ** lastnode = NULL;//마지막 노드
if (num_sen > 0)
{
for (int i = 1; i < num_sen; i++)
{
json_t * arr_data, *obj2, *arr2;
const char * key2;
int k;
json_array_foreach(obj, i, arr) {
//각 센서 array 시작
json_object_foreach(arr, key2, obj2) {
p_sen = (SENSOR_CONFIG **)malloc(sizeof(SENSOR_CONFIG));//먼저 생성하고 보자
arr_data = json_object_get(arr, key2);
if (_eq("id"))
{
fprintf(stderr, "can read id\n");
(*p_sen)->id = (int)json_integer_value(arr_data);
fprintf(stderr, "id:");
fprintf(stderr, "%d\n", (*p_sen)->id);
}
...
}
(*p_sen)->next = NULL;
if (firstnode == NULL) {//처음으로 실행되면
firstnode = p_sen;
lastnode = p_sen;
}
else {//처음이 아니면 뒤에 계속 추가. lastnode가 계속 바뀐다
(*lastnode)->next = p_sen;//원래 lastnode가 가리키는 곳이 새로 생성된 노드의 값을 가리키게 한 후,
lastnode = p_sen;//방금 생성한 노드가 맨 끝자리에 추가했으므로 방금 생성한 노드가 lastnode가 된다.
}
}//sensor array
}
}
else
{
mcfg->sensor_cfg = NULL;
}
}