255.255.255.255.0转/24 C语言

该博客主要探讨了IPv4子网掩码的处理,包括`dealMask`函数用于将十进制数转化为二进制表示,并进行位运算。此外,`transer`函数接收一个IPv4子网掩码字符串,将其转换为二进制并输出,同时计算并显示一共有多少个1。示例中使用了子网掩码`255.255.0.0`进行演示。

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <unistd.h>

#define MASK "255.255.0.0"
int dealMask(int x,int *buf,int ret){
        int i = ret * 8;
        while( (x/2) !=0 )
        {
                buf[i-1] = x%2;
                x = x/2;
                i--;
        }
        buf[i-1] = x%2;

        return i;

}
int transer(char *mark){
        char temp[8] = {0};
        int buf[32] = {0};
        char *str  = temp;
        char *head = mark;
        int count = 0;
        int ret = 1;
        int i = 0;
        int j = 0;
        while(*head != '\0'){
                *str = *head;
                if(*(head+1) == '.'|| *(head+1)== '\0'){
                        head = head +2;
                        dealMask(atoi(temp),buf,ret);
                        ret++;
                        memset(temp,0,sizeof(temp));
                        str = temp;
                        continue;
                }
                str++;
                head++;

        }
        for(i = 1;i <33;i++){
                if(buf[i-1] == 1){
                        j++;
                }
                printf("%d",buf[i-1]);
                if((i%8) == 0){
                        printf("\n");
                }
        }
        printf("%d",j);

}
int main(){
        transer(MASK);
        return 0;
}

uilibTrafficTest.c:130:5: error: conflicting types for 'trafficTestPacketSet' int trafficTestPacketSet(char *portListStr, char *rawPacket, UINT8 packetIndex, ^ In file included from uilibTrafficTest.c:30:0: ../../../../../../src/application/include/uilib/uilibTrafficTest.h:45:5: note: previous declaration of 'trafficTestPacketSet' was here int trafficTestPacketSet(char *portListStr, char *rawPacket, UINT8 packetIndex, ^ In file included from ../../../../../../src/platform/include/userPort/up_spec.h:11:0, from ../../../../../../src/platform/include/userPort/user_port_pvt.h:89, from ../../../../../../src/platform/include/userPort/user_port.h:88, from uilibTrafficTest.c:15: uilibTrafficTest.c: In function 'trafficTestPacketSet': uilibTrafficTest.c:154:118: error: pointer targets in passing argument 4 of 'tpConfig_objAddFieldStrForSet' differ in signedness [-Werror=pointer-sign] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_SRC_MAC, srcMac), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'INT8 * {aka signed char *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ In file included from ../../../../../../src/platform/include/userPort/up_spec.h:11:0, from ../../../../../../src/platform/include/userPort/user_port_pvt.h:89, from ../../../../../../src/platform/include/userPort/user_port.h:88, from uilibTrafficTest.c:15: uilibTrafficTest.c:155:118: error: pointer targets in passing argument 4 of 'tpConfig_objAddFieldStrForSet' differ in signedness [-Werror=pointer-sign] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_DST_MAC, dstMac), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'INT8 * {aka signed char *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ In file included from ../../../../../../src/platform/include/userPort/up_spec.h:11:0, from ../../../../../../src/platform/include/userPort/user_port_pvt.h:89, from ../../../../../../src/platform/include/userPort/user_port.h:88, from uilibTrafficTest.c:15: uilibTrafficTest.c:156:117: error: passing argument 4 of 'tpConfig_objAddFieldStrForSet' from incompatible pointer type [-Werror=incompatible-pointer-types] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_SRC_IP, srcIp), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'INT16 * {aka short int *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ In file included from ../../../../../../src/platform/include/userPort/up_spec.h:11:0, from ../../../../../../src/platform/include/userPort/user_port_pvt.h:89, from ../../../../../../src/platform/include/userPort/user_port.h:88, from uilibTrafficTest.c:15: uilibTrafficTest.c:157:117: error: passing argument 4 of 'tpConfig_objAddFieldStrForSet' from incompatible pointer type [-Werror=incompatible-pointer-types] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_DST_IP, dstIp), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'INT16 * {aka short int *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ cc1: all warnings being treated as errors CMakeFiles/uilib.dir/build.make:1313: recipe for target 'CMakeFiles/uilib.dir/uilibTrafficTest.c.o' failed make[4]: *** [CMakeFiles/uilib.dir/uilibTrafficTest.c.o] Error 1 CMakeFiles/Makefile2:107: recipe for target 'CMakeFiles/uilib.dir/all' failed make[3]: *** [CMakeFiles/uilib.dir/all] Error 2 Makefile:132: recipe for target 'all' failed make[2]: *** [all] Error 2 package/tplink/tplink-generic.mk:289: recipe for target '/project/fep_source_2/buildroot/build/ac5x_pro/build/tplink/uilib-1.0/.stamp_built' failed make[1]: *** [/project/fep_source_2/buildroot/build/ac5x_pro/build/tplink/uilib-1.0/.stamp_built] Error 2 Makefile:84: recipe for target '_all' failed make: *** [_all] Error 2 解释下报错
最新发布
10-28
<think>我们首先分析报错信息,主要分为两个部分: 1. 函数声明冲突错误 2. 函数调用时参数类型不匹配错误 ### 第一部分:函数声明冲突 ``` uilibTrafficTest.c:130:5: error: conflicting types for 'trafficTestPacketSet' int trafficTestPacketSet(char *portListStr, char *rawPacket, UINT8 packetIndex, ^ In file included from uilibTrafficTest.c:30:0: ../../../../../../src/application/include/uilib/uilibTrafficTest.h:45:5: note: previous declaration of 'trafficTestPacketSet' was here int trafficTestPacketSet(char *portListStr, char *rawPacket, UINT8 packetIndex, ^ ``` **错误原因**: - 在头文件`uilibTrafficTest.h`中已经声明了函数`trafficTestPacketSet`,但是在源文件`uilibTrafficTest.c`中再次定义时,与头文件中的声明不一致(可能是参数类型或数量不同)。 - 这里从报错信息看,两个声明看起来相同,但可能在其他地方(比如包含的头文件)对`UINT8`的定义不同,导致在编译不同单元时`UINT8`被解释为不同的类型,从而引起冲突。 **解决方案**: - 检查`uilibTrafficTest.c`和`uilibTrafficTest.h`中`trafficTestPacketSet`函数的声明是否完全一致,包括参数类型和返回类型。 - 确保两个声明中使用的自定义类型(如`UINT8`)在各自上下文中是相同的类型定义。可能需要在源文件中包含相同的头文件来保证类型定义一致。 ### 第二部分:参数类型不匹配(多个错误) #### 错误1和2:符号性不匹配(-Werror=pointer-sign) ``` uilibTrafficTest.c:154:118: error: pointer targets in passing argument 4 of 'tpConfig_objAddFieldStrForSet' differ in signedness [-Werror=pointer-sign] ... tpConfig_objAddFieldStrForSet(..., srcMac), ...); ^ note: expected 'const char *' but argument is of type 'INT8 * {aka signed char *}' ``` **错误原因**: - 函数`tpConfig_objAddFieldStrForSet`的第四个参数要求是`const char*`,但实际传入的是`INT8*`(即`signed char*`)。 - 在C语言中,`char*`和`signed char*`虽然都是字符指针,但它们是不同的指针类型,编译器会因此报错(尤其是开启了`-Wpointer-sign`,并且用`-Werror`将警告视为错误)。 #### 错误3和4:指针类型不兼容(-Werror=incompatible-pointer-types) ``` uilibTrafficTest.c:156:117: error: passing argument 4 of 'tpConfig_objAddFieldStrForSet' from incompatible pointer type ... tpConfig_objAddFieldStrForSet(..., srcIp), ...); note: expected 'const char *' but argument is of type 'INT16 * {aka short int *}' ``` **错误原因**: - 同样调用`tpConfig_objAddFieldStrForSet`函数,但这里传入的`srcIp`是`INT16*`(即`short int*`),而函数要求`const char*`,显然这两种指针类型完全不兼容。 ### 根本问题分析 函数`tpConfig_objAddFieldStrForSet`要求第四个参数是字符串(即`const char*`),但调用时却传入了不同类型的指针: - `srcMac`是`INT8*`(即`signed char*`) - `dstMac`也是`INT8*` - `srcIp`是`INT16*`(即`short int*`) - `dstIp`也是`INT16*` 这显然是不正确的。根据函数名`objAddFieldStrForSet`,这个函数应该是用来设置字符串类型字段的,因此需要传入字符串(以空字符结尾的字符数组)。而这里传入的是其他类型的指针(可能是二进制表示的MAC地址和IP地址),导致类型不匹配。 ### 解决方案 1. **转换参数类型**:将非字符串参数转换为字符串形式。例如,MAC地址和IP地址通常需要格式化为字符串(如"00:11:22:33:44:55"或"192.168.1.1")。 2. **修改函数调用**:确保传入的参数是字符串指针。 #### 示例修复(针对MAC地址): ```c // 假设srcMac是INT8数组(6个元素),表示MAC地址 char macStr[18]; // 足够容纳MAC字符串(17字符+1结尾空字符) snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", (unsigned char)srcMac[0], (unsigned char)srcMac[1], (unsigned char)srcMac[2], (unsigned char)srcMac[3], (unsigned char)srcMac[4], (unsigned char)srcMac[5]); // 然后使用macStr调用函数 tpConfig_objAddFieldStrForSet(..., CFG_TRAFFIC_TEST_F_SRC_MAC, macStr); ``` #### 针对IP地址(假设是16位整数表示的IPv4地址): 注意:这里需要明确IP地址的表示方式。如果是16位整数数组(每个整数表示一个IP地址段?),则需要正确解析。 ```c // 假设srcIp是INT16数组(4个元素?),表示IPv4地址 char ipStr[16]; // 足够容纳IPv4字符串(如"255.255.255.255") snprintf(ipStr, sizeof(ipStr), "%d.%d.%d.%d", (int)(srcIp[0] & 0xFF), (int)(srcIp[1] & 0xFF), (int)(srcIp[2] & 0xFF), (int)(srcIp[3] & 0xFF)); // 然后调用 tpConfig_objAddFieldStrForSet(..., CFG_TRAFFIC_TEST_F_SRC_IP, ipStr); ``` ### 其他注意事项 - 由于编译选项开启了`-Werror`(将警告视为错误),必须消除所有警告。 - 需要检查所有调用`tpConfig_objAddFieldStrForSet`的地方,确保传入的第四个参数是字符串。 ### 总结 主要的错误有两个方面: 1. 函数`trafficTestPacketSet`的声明与头文件中的声明冲突(需要统一声明)。 2. 调用`tpConfig_objAddFieldStrForSet`时传递了错误类型的参数(需要将MAC和IP地址格式化为字符串再传入)。 §§相关问题§§ 1. C语言中函数声明与定义不一致会导致哪些类型的编译错误? 2. 如何正确地将MAC地址从字节数组格式化为字符串表示? 3. 在C语言中,为什么`char*`和`signed char*`被视为不同的指针类型? 4. 使用`snprintf`函数进行格式化字符串时,如何避免缓冲区溢出? 5. 在跨平台代码中,如何确保自定义类型(如UINT8, INT16)在不同编译器中的一致性? </think>### 报错解释及解决方案 #### 错误1:函数声明冲突 ``` error: conflicting types for 'trafficTestPacketSet' note: previous declaration of 'trafficTestPacketSet' was here ``` **原因**: 1. 头文件(`uilibTrafficTest.h:45`)和源文件(`uilibTrafficTest.c:130`)中的函数声明不一致 2. 可能原因: - 修改了函数参数但未更新头文件 - 头文件和源文件包含顺序导致类型定义差异 - 编译单元之间的可见性不一致 **解决方案**: ```c // 步骤1:检查头文件声明 // uilibTrafficTest.h 中应保持声明为: int trafficTestPacketSet(char *portListStr, char *rawPacket, UINT8 packetIndex, ...); // 步骤2:确保源文件实现与头文件声明严格一致 // uilibTrafficTest.c 中实现应完全匹配 ``` --- #### 错误2:指针符号性不匹配(MAC地址) ``` error: pointer targets in passing argument 4 ... differ in signedness note: expected 'const char *' but argument is of type 'INT8 *' ``` **原因**: 1. `tpConfig_objAddFieldStrForSet()` 要求 `const char*` 类型参数 2. 实际传入的 `srcMac/dstMac` 是 `INT8*` (即 `signed char*`) 3. 符号性差异:`char*` (可能有符号) vs `signed char*` (明确有符号) **解决方案**: ```c // 方法1:显式类型转换(快速修复) tpConfig_objAddFieldStrForSet(..., (const char*)srcMac); // 方法2:转换MAC地址为字符串格式(推荐) char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", srcMac[0] & 0xFF, srcMac[1] & 0xFF, ...); tpConfig_objAddFieldStrForSet(..., macStr); ``` --- #### 错误3:指针类型不兼容(IP地址) ``` error: passing argument 4 from incompatible pointer type note: expected 'const char *' but argument is of type 'INT16 *' ``` **原因**: 1. 函数要求字符串指针(`const char*`) 2. 传入的 `srcIp/dstIp` 是 `INT16*` (即 `short*`) 3. 二进制整数指针与字符串指针完全不兼容 **解决方案**: ```c // 必须将IP地址转换为字符串 char ipStr[16]; // 假设是IPv4地址(4个字节) snprintf(ipStr, sizeof(ipStr), "%d.%d.%d.%d", (srcIp[0] >> 8) & 0xFF, srcIp[0] & 0xFF, (srcIp[1] >> 8) & 0xFF, srcIp[1] & 0xFF); tpConfig_objAddFieldStrForSet(..., ipStr); ``` --- ### 根本问题分析 1. **API设计缺陷**: - `tpConfig_objAddFieldStrForSet` 明确要求字符串参数 - 调用方传递了原始二进制数据指针(MAC/IP地址) 2. **类型系统冲突**: ```c // 网络层使用二进制表示 INT8 mac[6]; // 有符号字节数组 INT16 ip[2]; // 16位整数表示IPv4 // 配置层要求字符串 const char *mac_str = "00:11:22:33:44:55"; const char *ip_str = "192.168.1.1"; ``` 3. **编译严格性**: - `-Werror=pointer-sign` 和 `-Werror=incompatible-pointer-types` 将警告视为错误 - `-Werror` 导致编译失败
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值