| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 753 人关注过本帖
标题:萌新求助,控制台程序出现异常输出
只看楼主 加入收藏
鱼头豆腐汤
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2020-10-21
结帖率:87.5%
收藏
已结贴  问题点数:11 回复次数:2 
萌新求助,控制台程序出现异常输出
程序目的是查找这些数据中,有几个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;
}
搜索更多相关主题的帖子: int 出现 printf 输出 控制台 
2022-11-20 23:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:11 
问题看不懂,代码看不懂,描述的现象也看不懂

程序代码:
#include <stdio.h>
#include <string.h>

int main( void )
{
    unsigned char buf[] =
    {
        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 
    };

    struct Player
    {
        int id;
        int level;
    } player_tag = { 1, 8 };

    const size_t buf_size = sizeof(buf)/sizeof(*buf);
    const size_t tag_size = sizeof(struct Player);
    for( const unsigned char* p=buf; p+tag_size<=buf+buf_size; ++p )
    {
        if( memcmp(p,&player_tag,tag_size) == 0 )
        {
            printf( "%p: ", p );

            struct Player tmp;
            memcpy( &tmp, p, tag_size );
            printf( "%x %x\n", tmp.id, tmp.level );
        }
    }

    return 0;
}
2022-11-21 09:27
鱼头豆腐汤
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2020-10-21
收藏
得分:0 
回复 2楼 rjsp
谢谢大佬
2022-11-21 14:44
快速回复:萌新求助,控制台程序出现异常输出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015152 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved