萌新求助,控制台程序出现异常输出
程序目的是查找这些数据中,有几个id=1 level=8的结构体信息,结构体塞代码里了。程序一共有三行输出(包括一行换行),控制台本应输出两行结果,但是出现了第三行不正确的输出。随意注掉任何一条输出结果的语句,控制台就会正常显示。
换种输出方式也出现一样的问题。
程序代码:
//结构体 typedef struct TagPlayer { int id; int level; }Player; //输出方式1 printf("%x %x\n",*(y-1),*(y)); printf("%x %x\n",y-1,y); //输出方式2 printf("%x %x\n",*(y-1),y-1); printf("%x %x\n",*(y),y);
程序代码:
#include "stdafx.h" char arr[]= { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09, 0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00, 0x00,0x33,0x01,0x00,0x00,0x08,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00, 0x00,0x00,0x64,0x01,0x00,0x00,0x00,0x08,0x00,0x00, 0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00, 0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00, 0x00,0x02,0x57,0x4F,0x57,0x00,0x06,0x08,0x00,0x00, 0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00, 0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00 }; typedef struct TagPlayer { int id; int level; }Player; int main() { char* x=arr; int len = sizeof(arr) / sizeof(arr[0]); while(x-arr<len-7) { int* y=(int*)x; if(*y==1) { y++; if(*y==8) printf("%x %x\n",*(y-1),*(y)); printf("%x %x\n",y-1,y); printf("\n"); } x++; } return 0; }