NBB_PUT_SHORT 这个宏是按char* 类型算指针的实际工作中,没有注意这一点,输入指针类型 强转为了 SHORT* 导致 填充的内容错误 这是第一次在工作中遇到 指针类型相关的问题值得记录一笔
1 // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <stdio.h> 6 #include <string.h> 7 #include <stdlib.h> 8 9 typedef unsigned char NBB_BYTE; /* 8 bits */ 10 typedef signed char NBB_CHAR; /* */ 11 typedef signed short NBB_SHORT; /* 2 byte signed integer */ 12 typedef unsigned short NBB_USHORT; /* 2 byte unsigned integer */ 13 typedef signed long NBB_LONG; /* 4 byte signed integer */ 14 typedef unsigned long NBB_ULONG; /* 4 byte unsigned integer */ 15 typedef signed int NBB_INT; /* >= 2 byte signed integer (machine */ 16 /* word) */ 17 typedef unsigned int NBB_UINT; /* >= 2 byte unsigned integer */ 18 /* machine word) */ 19 typedef int NBB_BOOL; /* boolean */ 20 typedef void NBB_VOID; /* void if supported, char otherwise */ 21 22 typedef NBB_BYTE* BFD_PUC_ALARM_INFO; 23 24 BFD_PUC_ALARM_INFO g_pucAlarmInfo; /* ״̬Éϱ¨ÐÒéÕ» */ 25 26 #define NBB_PUT_SHORT(B, S) 27 (B)[0] = (unsigned char)((S) >> 8); 28 (B)[1] = (unsigned char)((S) & 0xFF) 29 30 void init() 31 { 32 g_pucAlarmInfo = (BFD_PUC_ALARM_INFO) malloc(1000); 33 memset(g_pucAlarmInfo, 0, 1000); 34 } 35 int main() 36 { 37 NBB_BYTE ucLocalSlot = 0; 38 NBB_BYTE ucMainSlot = 0;/* Ö÷ÓÃÖ÷¿ØÅ̵IJÛλºÅ */ 39 NBB_BYTE *pucInfoTmp = NULL; 40 NBB_USHORT *pusData = NULL; 41 NBB_ULONG ulDataLenth = 0; 42 43 init(); 44 45 pucInfoTmp = g_pucAlarmInfo + 24; 46 pusData = (char *)pucInfoTmp; 47 *pusData = 0x0001; 48 #if 0 49 NBB_PUT_SHORT(pusData, 0x0001); /* ÌîдÀàÐÍ */ 50 #else 51 pusData[0] =(unsigned char)( 0x0001 >> 8); 52 pusData[1] =(unsigned char)( 0x0001 & 0xff); 53 54 #endif 55 pucInfoTmp = pucInfoTmp + 2; 56 return 0; 57 }
时间: 2024-11-05 22:17:14