char cust_sw_rev[100] = ""; const char substr = "-NETRANI_GENALL_PACK"; // 需要删除的子串 strcpy(cust_sw_rev, mob_sw_rev); char p = strstr(cust_sw_rev, substr); if (p!= NULL) { memmove(p, p + strlen(substr), strlen(p + strlen(substr)) + 1); // 删除子串 } int len = strlen(cust_sw_rev); if (len > 40) { cust_sw_rev[40] = '\0'; } /* char p = strrchr(mob_sw_rev, '-'); snprintf((char)cust_sw_rev, 22, "%s", (char*)mob_sw_rev); sprintf((char*)(cust_sw_rev+21), "%s", p); */ 使用上面代码修改参与编译,当前报错为: error: redefinition of 'len' with a different type: 'int' vs 'uint16' (aka 'unsigned short'),我们应该如何修改,请给出修改后的代码
时间: 2024-02-24 12:59:16 浏览: 96
cust_global.rar_DEA-Solver-Lv_I Do! I Do!
在这段代码中,同样也存在重定义的问题。不仅是变量名`len`的类型问题,还有`substr`的类型问题。下面是修改后的代码:
```
char cust_sw_rev[100] = "";
const char* substr = "-NETRANI_GENALL_PACK"; // 需要删除的子串(改为指针类型)
strcpy(cust_sw_rev, mob_sw_rev);
char* p = strstr(cust_sw_rev, substr);
if (p != NULL) {
memmove(p, p + strlen(substr), strlen(p + strlen(substr)) + 1); // 删除子串
}
size_t len = strlen(cust_sw_rev); // 将int类型改为size_t类型
if (len > 40) {
cust_sw_rev[40] = '\0';
}
```
这样就解决了变量类型重定义的问题,同时还将`substr`的类型从`char`改为了指向`const char`类型的指针。
阅读全文