1
比方說,我有一個這樣的結構:傳遞結構條目的字符串,反之亦然
typedef struct {
unsigned s_i, s_j;
unsigned d_i, d_j;
} obs_t
我要定義一個函數:
obs_t * string_to_struct (char * s);
其內容的字符串格式化,例如,如下所示:「1 1 4 6」並將四個數字傳遞給struct obs_t。到目前爲止,我沒能避免分段錯誤的,我使用下面的代碼:
obstacle_t * string_to_obstacle (char * s){
obstacle_t *ostacolo = malloc(sizeof(obstacle_t));
unsigned int **format;
sscanf(s, "%u %u %u %u", format[0], format[1], format[2], format[3]);
ostacolo -> s_i = *format[0];
ostacolo -> s_j = *format[1];
ostacolo -> d_i = *format[2];
ostacolo -> d_j = *format[3];
}
變量format是一個指針。但它指向哪裏? –