标题:萌新上路,代码写的很恶心,可读性差,大佬勿喷
只看楼主
鱼头豆腐汤
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2020-10-21
结帖率:100%
 问题点数:0 回复次数:4 
萌新上路,代码写的很恶心,可读性差,大佬勿喷
#include "stdafx.h"

char x[]=
{
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,                    
    0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,                    
    0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,                    
    0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,                    
    0x00,0x00,0x64,0x10,0x00,0x00,0x00,0x00,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                    
};

char y[]={"WOW"};
int l=0;
char* FindRoleNameAddr(char* x,char* y)   
{
    int n;
    char* e;
    char* a=&y[0];
    char* b=&x[0];
    for(int i=0;i<100;i++)
    {
        n=0;
        y=a;
        x=b++;
        e=x;
        while(*y!=0)
        {
            if(*y==*x)
            {
                n++;
                x++;
                y++;
            }else
            {
            break;
            }
        }
        if(n==3)
        {
            l++;
            return e;
        }
    }
    return 0;
}               

int main()
{
    int* t=(int*)FindRoleNameAddr(x,y);
    printf("%x\n",t);
    char* f=FindRoleNameAddr(x,y);
    while(*f!=0)
    {
        printf("%c",*f);
        f++;
    }
    printf("\n%d",l);
    return 0;
}
搜索更多相关主题的帖子: 可读性 printf return char int 
2022-11-18 22:40
apull
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:三体星系
等 级:版主
威 望:185
帖 子:1404
专家分:8479
注 册:2010-3-16
得分:0 
程序代码:
#include <stdio.h>

int main()
{
    char x[] =
        {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x07, 0x09,
            0x00, 0x20, 0x10, 0x03, 0x03, 0x0C, 0x00, 0x00, 0x44, 0x00,
            0x00, 0x33, 0x00, 0x47, 0x0C, 0x0E, 0x00, 0x0D, 0x00, 0x11,
            0x00, 0x00, 0x00, 0x02, 0x64, 0x00, 0x00, 0x00, 0xAA, 0x00,
            0x00, 0x00, 0x64, 0x10, 0x00, 0x00, 0x00, 0x00, 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};

    char y[] = {"WOW"};

    int len = sizeof(x) / sizeof(x[0]);

    char *a = x;
    int i, l = 0;
    //-- 辅助,可删
    for (i = 0; i < 100; i++)
        printf("%c %p\n", x[i], x + i);
    printf("\n");
    //--
    while (a - x < len - 2)
    {
        for (i = 0; i < 3 && *a == y[i]; i++, a++) ;

        if (i == 3)
        {
            printf("%p\n", a - 3);
            l++;
        }
        a++;
    }
    printf("%d", l);
}


2022-11-18 23:51
鱼头豆腐汤
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2020-10-21
得分:0 
回复 2楼 apull
int len = sizeof(x) / sizeof(x[0]);

while(a-x<len-2)
这两句不是太懂,可以麻烦大佬讲一下吗?
2022-11-19 00:14
apull
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:三体星系
等 级:版主
威 望:185
帖 子:1404
专家分:8479
注 册:2010-3-16
得分:0 
int len = sizeof(x) / sizeof(x[0]); 计算数组长度,x总字节数/x元素字节数=x元素数量

while(a-x<len-2)  a-x是指针a相对x的偏移量,相当于a[j]的下标量j。
查找到倒数第3个字符就可以了,往后没必要查了,长度比要查询的小。
2022-11-19 08:43
鱼头豆腐汤
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2020-10-21
得分:0 
回复 4楼 apull
明白了,谢谢大佬
2022-11-19 09:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-510698-1-1.html




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

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