我有以下代码:
char *temp_sentence, *second_temp_ptr;
char *token;
int token_no = 0;
temp_sentence = strdup(sentence);
second_temp_ptr = temp_sentence;
token = strsep (&second_temp_ptr,",");
while (token != NULL) {
/*if a sentence has missing data then make that clear by settings it's value to
* <EMPTY>*/
if(strlen(token)==0){
token = "<EMPTY>";
}
switch(token_no){
case 0:
gga_ptr->sentence_id = token;
printf("%s,",gga_ptr->sentence_id);
break;
case 1:
/*atoi converts a string to an int, well a c string anyways so a char* */
gga_ptr->time_stamp = atoi(token);
printf("%d,",gga_ptr->time_stamp);
break;
case 2:
/*strtod coverts a string to a double, well a c string anyways so a char* */
gga_ptr->latitude = strtod(token, NULL);
printf("%f,",gga_ptr->latitude);
break;
case 3:
//gga_ptr->north_south_id = token;
//printf("%s,",gga_ptr->north_south_id);
break;
case 4:
gga_ptr->longitude = strtod(token, NULL);
printf("%f,",gga_ptr->longitude);
break;
case 5:
gga_ptr->east_west_id = token;
//printf("%s,",gga_ptr->east_west_id);
break;
case 6:
gga_ptr->quality = atoi(token);
printf("%d,",gga_ptr->quality);
break;
case 7:
gga_ptr->no_of_satellites = atoi(token);
printf("%d,",gga_ptr->no_of_satellites);
break;
case 8:
gga_ptr->horizontal_dillution = strtod(token, NULL);
printf("%f,",gga_ptr->horizontal_dillution);
break;
case 9:
gga_ptr->altitude = strtod(token, NULL);
printf("%f,",gga_ptr->altitude);
break;
case 10:
gga_ptr->altitude_units = token;
//printf("%s,",gga_ptr->altitude_units);
break;
case 11:
gga_ptr->geodial_seperation = strtod(token, NULL);
printf("%f,",gga_ptr->geodial_seperation);
break;
case 12:
gga_ptr->geodial_seperation_units = token;
//printf("%c,",gga_ptr->geodial_seperation_units);
break;
case 13:
/*This is never used in the sentenced given*/
gga_ptr->age_of_data_in_seconds = token;
printf("%f,",gga_ptr->age_of_data_in_seconds);
break;
case 14:
gga_ptr->checksum = token;
printf("%s,",gga_ptr->checksum);
break;
}
token_no++;
token = strsep (&second_temp_ptr, ",");
}
exit(1);
free(temp_sentence);
}
和下面的 gga_sentence 结构:
typedef struct gga_sentence{
char *untouched_sentence;
char *sentence_id;
int time_stamp;
double latitude;
char north_south_id;
double longitude;
char east_west_id;
int quality;
int no_of_satellites;
double horizontal_dillution;
double altitude;
char altitude_units;
double geodial_seperation;
char geodial_seperation_units;
char *age_of_data_in_seconds;
char *checksum;
}gga_sentence;
上面的代码按预期工作,直到我尝试打印出代表我的结构的 char 成员的值,所以在 case-switch 情况下 3,5,10,12 会产生段错误,如果我删除这些,其余结构成员按预期打印。
函数输入:
$GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72
代码输出(不包括情况3、5、10、12的打印语句):
$GPGGA,151019,5225.962700,401.162400,1,9,1.000000,38.900000,51.100000,51.100000,0000*72
预期输出应与输入相同,但这些 char 成员导致段错误。
打印 token 时:
token: $GPGGA
token: 151019.000
token: 5225.9627
token: N
token: 00401.1624
token: W
token: 1
token: 09
token: 1.0
token: 38.9
token: M
token: 51.1
token: M
token: <EMPTY>
token: 0000*72
gdb 的输出,回溯:
(gdb) where
#0 0x00007ffff7a5ef90 in _IO_vfprintf_internal (s=<optimised out>, format=<optimised out>,
ap=ap@entry=0x7fffffffd8b8) at vfprintf.c:1655
#1 0x00007ffff7a65ff9 in __printf (format=<optimised out>) at printf.c:34
#2 0x0000000000400daf in initiate_gga_values (gga_ptr=0x603250,
sentence=0x603250 "$GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72\r\n") at stream_1_parser.c:103
#3 0x0000000000400bee in read_stream_1 (stream=0x603010) at stream_1_parser.c:40
#4 0x0000000000400a42 in main (argc=1, argv=0x7fffffffdf78) at driver.c:28
stream_1_parser.c:40 是对给定函数的调用,driver.c:28 是对 stream_1_parser.c:40 的调用
有没有人有什么想法?
干杯, 克里斯。
最佳答案
在问题案例中尝试“%c”而不是“%s”:
printf("%c,"...
当你分配给结构时,这样做:
case 5:
gga_ptr->east_west_id = *token;
对于问题案例,将“token”更改为“*token”。
关于为我的结构的 char 成员赋值时出现 C 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22313417/