be carefull in c

本文通过一个C语言示例介绍了使用数组和宏定义时可能遇到的问题,特别是当涉及到类型转换时,如何避免常见的错误。该示例展示了当宏定义的数值与负数进行比较时可能出现的意外行为。
#include <stdio.h>

int arr[]={0,1,2,3,4};
#define TOTAL_ELEMENTS (sizeof(arr)/sizeof(arr[0]))

int main(int argc,char *argv[])
{
    int d=-1,x;
    if(d <= (int)TOTAL_ELEMENTS-1)
    //这里如果是if(d <= TOTAL_ELEMENTS-1)的话,下面的语句不会执行
    //because  TOTAL_ELEMENT is unsigned int
    //so  d is change to unsigned, while d=-1 it change to oxFFFFFFFF
    //so when compare tow numbers be careful it's type changing
    {
        x = arr[d+1];
    }
    printf("%d",x);
}
帮我判断这个结构中哪个成员是0x1a4的偏移,64位系统:struct fkbuff { /* List pointer must be the first field */ union { FkBuff_t * list; /* SLL of free FKBs for cloning */ FkBuff_t * master_p; /* Clone FKB to point to master FKB */ atomic_long_t users; /* (private) # of references to FKB */ }; union { /* Use _is_kptr_ to determine if ptr */ union { void *ptr; struct blog_t *blog_p; /* Pointer to a blog */ uint8_t *dirty_p; /* Pointer to packet payload dirty incache*/ uint32_t flags; /* Access all flags */ }; /* * First nibble denotes a pointer or flag usage. * Lowest two significant bits denote the type of pinter * Remaining 22 bits may be used as flags */ struct { uint32_t ptr_type : 8;/* Identifies whether pointer */ uint32_t unused :21;/* Future use for flags */ uint32_t in_skb : 1;/* flag: FKB passed inside a SKB */ uint32_t other_ptr: 1;/* future use, to override another pointer*/ uint32_t dptr_tag : 1;/* Pointer type is a dirty pointer */ }; }; uint8_t * data; /* Pointer to packet data */ union { /* here the bits 31-24 are valid only for native fkbs's * these bits bits will be cleared when using fkbInSkb * Note that it is critical to have the Little Endian/Big endian * declaration since FKB will use length as bit field and SKB will use * length as a word Need to maintain the same bit positions across MIPS * and ARM. */ struct{ BE_DECL( uint32_t rx_csum_verified:1; uint32_t spdtst:1; uint32_t reserved:6; uint32_t len:24; /* Packet length */ ) LE_DECL( uint32_t len:24; uint32_t reserved:6; uint32_t spdtst:1; uint32_t rx_csum_verified:1; ) }; uint32_t len_word; }; union { /* only the lower 32 bit in mark is used in 64 bit system, * but we delcare it as unsigned long for the ease for fcache * to handle it in different architecture, since it is part * of union with a dst_entry pointer */ unsigned long mark; /* Custom arg1, e.g. tag or mark field */ void *queue; /* Single link list queue of FKB | SKB */ void *dst_entry; /* rtcache entry for locally termiated pkts */ uint32_t fc_ctxt; /* hybrid flow cache context */ }; union { uint32_t priority; /* Custom arg2, packet priority, tx info */ wlFlowInf_t wl; /* WLAN Flow Info */ uint32_t flowid; /* used for locally terminated pkts */ }; RecycleFuncP recycle_hook; /* Nbuff recycle handler */ union { /* recycle hook for Clone FKB is used in DHD pointing to extra info * BE CAREFULL when using this recyle_context for free etc.... */ void *dhd_pkttag_info_p; unsigned long recycle_context; /* Rx network device/channel or pool */ uint32_t fpm_num; }; } ____cacheline_aligned; /* 2 cache lines wide */
最新发布
09-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值