简体   繁体   English

将值分配给我的struct的char成员时C分段错误

[英]C segmentation fault when assigning values to the char members of my struct

I have the following code: 我有以下代码:

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);

}

and the following gga_sentence structure: 以及以下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;

The above code works as expected, untill i try to print out the values that represent char members of my structure, so in the case-switch cases 3,5,10,12 produce a segmentation fault, if i remove these, the rest of the structure members print as expected. 上面的代码按预期工作,直到我尝试打印出代表我的结构的char成员的值,所以在case-switch情况3,5,10,12产生分段错误,如果我删除这些,其余的结构成员按预期打印。

function input: 功能输入:

$GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72

code output (exluding the print statements in cases 3,5,10,12): 代码输出(排除案例3,5,10,12中的打印语句):

$GPGGA,151019,5225.962700,401.162400,1,9,1.000000,38.900000,51.100000,51.100000,0000*72

expected output should be the same as the input, but these char members are casuing segmentation faults. 预期输出应该与输入相同,但这些char成员正在进行分段错误。

when the tokens are printed: 打印代币时:

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

output from gdb where, traceback: 从gdb输出where,traceback:

(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 is a call to the function given and driver.c:28 is a call to stream_1_parser.c:40 stream_1_parser.c:40是对给定函数的调用,driver.c:28是对stream_1_parser.c的调用:40

Does anybody have any ideas? 有人有什么想法吗?

Cheers, Chris. 干杯,克里斯。

Try "%c" instead of "%s" in the problem cases: 在问题情况下尝试“%c”而不是“%s”:

printf("%c,"...

And when you assign to the structure do this: 当你分配给结构时,执行以下操作:

case 5:
  gga_ptr->east_west_id = *token;

Change "token" to "*token" for the problem cases. 针对问题情况将“令牌”更改为“*令牌”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM