首页 > 解决方案 > 即使 IF 语句为假,字符指针数组也会被覆盖

问题描述

我正在通过指定的分隔符分解字符串并尝试typedef根据第一个字符串修改结构。我意识到,当我单步执行代码时,typedef即使 if 语句为假,结构也会被覆盖。我希望它保留之前 for 循环迭代的旧值,但事实并非如此。
我还在考虑创建保存 str 值的局部字符变量,以便我相应地更新它们并将它们的值分配给coordtypedef。但后来似乎创造了太多的变量。

我的愿望是typedef仅在字符串以某个字符串开头时才更新标题。否则,打印 headingPrev 中可用的内容。

typedef struct {
    char* utc;
    char* lat;
    char* lat_dir;
    char* lon;
    char* lon_dir;
    char* speed_kn;
    char* heading;
} CoordinatesHandleTypeDef;

const char *str_gprmc[7] = {
    "$GPRMC,125812.50,A,5741.1245547,N,01158.9460229,E,10.324,207.1,270319,0.0,E,A*0F",
    "$GPRMC,130019.00,A,5741.5393572,N,01158.6608248,E,14.013,331.8,270319,0.0,E,A*0F",
    "$GPRMC,130019.50,A,5741.5414303,N,01158.6591608,E,15.498,331.8,270319,0.0,E,A*07",
    "$GPHDT,3.0979,T*01",
    "$GPRMC,130132.00,A,5741.6055487,N,01158.3862843,E,9.536,174.0,270319,0.0,E,A*35",
    "$GPRMC,130132.50,A,5741.6042334,N,01158.3862498,E,10.783,172.1,270319,0.0,E,A*00",
    "$GPHDT,357.8596,T*06"
};

CoordinatesHandleTypeDef coord = {0};

    // Loop through for every string that comes in. Imitate USART End of Line....then process the string

    // We gonna keep the heading parameter out of the loop to get updated only once the data is available
    // This is because according to the datasheet, its updated onchanged. i.e when there is a detection in heading mismatches
    char* headingPrev = NULL;
    uint8_t* str[8] = { NULL };
    uint8_t temp[50] = { NULL };

    for (size_t k = 0; k < ARRAY_SIZE(str_gprmc); k++)
    {
        size_t maxStorableTokens = 0;

        // store the string in a local variable to avoid access violation exceptions for read/write operations
        strcpy(temp, str_gprmc[k]);

        for (uint8_t *ptr_token = strtok(temp, ","); ptr_token; ptr_token = strtok(NULL, ","))
        {
            if (maxStorableTokens >= 0x08) break;

            str[maxStorableTokens++] = ptr_token;
        }

        // if the string at index[0] is $GPRMC
        if (strcmp(str[0], "$GPRMC") == 0)
        {
            coord.utc       = str[1];
            coord.lat       = str[3];
            coord.lat_dir   = str[4];
            coord.lon       = str[5];
            coord.lon_dir   = str[6];
            coord.speed_kn  = str[7];

            coord.heading   = headingPrev;
        }
        else if(strcmp(str[0], "$GPHDT") == 0) 
            coord.heading = headingPrev = str[1]; 

        // Print out the updated contents of cood after every loop
        printf("UTC : %s, \tLONG : %s, \tLONG_DIR : %s, \tLAT : %s, \tLAT_DIR : %s, \tSPEED(Kn) : %s, \tHEADING : %s\n", 
            coord.utc, coord.lat, coord.lat_dir, coord.lon, coord.lon_dir, coord.speed_kn, headingPrev);
    }

结果打印

标签: cpointerstypedef

解决方案


你有

char* headingPrev = NULL;
    uint8_t* str[8] = { NULL };

这里 headingPrev 和 str 是指针,它们只能保存地址而不是实际的 srings(或 char 数组)。所以当你尝试

coord.heading = headingPrev = str[1]; 

这个语句headingPrev 只指向str[1]。这相当于#define headingPrev &str[1]which 显然不是你的意图。您需要 headingPrev 来保存 str[1] 的值而不是其地址。所以,你需要做的是为 headingPrev 分配内存。char HeadingPrev[20]如果 str[1] 的大小不固定,您可以执行或使用 malloc/calloc 进行动态内存分配。然后您必须使用 . 将 str[1] 复制到 headingPrev strcpy()。所以最终的代码是

 if (strcmp(str[0], "$GPRMC") == 0)
        {
            coord.utc       = str[1];
            coord.lat       = str[3];
            coord.lat_dir   = str[4];
            coord.lon       = str[5];
            coord.lon_dir   = str[6];
            coord.speed_kn  = str[7];

            coord.heading   = headingPrev; //<---- This would still work since headingPrev would be same as &headingPrev[0] even after allocating memory
        }
        else if(strcmp(str[0], "$GPHDT") == 0) 
        {
           strcpy(headingPrev, str[1]);
            coord.heading = headingPrev; 
        }

您也可以放置coord.heading = headingPrev;if-else块之外,因为它同时出现在 if 和 else 块中。


推荐阅读