将C指针作为数组索引引用两次

时间:2016-05-10 01:36:58

标签: c pointers

我有一个C程序,我试图在一串文本上进行内存转储。我想打印一定数量的字节,然后转到下一行并再打印一些。在我的第二个for循环中,我收到一个错误,因为我正在写内存。在我的第二个循环中,我想打印与第一个循环中打印的完全相同的字符,但是ascii而不是hex:

void    dump(void *, int);
char * strcatp(char *, char *);

void
main()
{
    char *str;
    str = "this is an array of bytes, of which we will read some of them into our function";

    print("len=%ld\n", strlen(str) +1);
    dump(str, strlen(str) + 1);
}
void
dump(void *a, int len)
{
    char *p;
    int i, j, pos, rcount;

    p = (char *) a;
    rcount = 5;
    pos = 0;

    for(i=0;i<len;i++){
        print("%p ", p[pos]);

        if(pos + rcount >  len)
            rcount = len - pos;
        if(pos == len)
            return; 
        for(j = pos; j < pos + rcount; j++){
            print("%02x ", p[j]);
        }
        print("   *");
        for(j = pos; j < pos + rcount; j++){
            if(isalpha(p[j]))
                print("%s ", p[j]);
            else 
                print(".");
            //print("%s ", p[j]);
        }
        print("*\n");
        pos += rcount;
    }
}

为什么我一直在写记忆?我没有推进我所知道的指针。

1 个答案:

答案 0 :(得分:2)

程序在没有警告的情况下进行编译非常重要,我们会从您的代码中收到警告:

$ gcc main.c 
main.c: In function ‘dump’:
main.c:28:16: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int’ [-Wformat=]
         printf("%p ", p[pos]);
                ^
main.c:40:24: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
                 printf("%s ", p[j]);

我们应该确保在调用函数时类型匹配。如果我修复了你的错误,看起来你的程序很好,并且没有错误消息。

void    dump(void *, int);
char * strcatp(char *, char *);

void
main()
{
    char *str;
    str = "this is an array of bytes, of which we will read some of them into our function";

    printf("len=%ld\n", strlen(str) +1);
    dump(str, (unsigned) strlen(str) + 1);
}
void
dump(void *a, int len)
{
    char *p;
    int i, j, pos, rcount;

    p = (char *) a;
    rcount = 5;
    pos = 0;

    for(i=0;i<len;i++){
        printf("%s ", &p[pos]);

        if(pos + rcount >  len)
            rcount = len - pos;
        if(pos == len)
            return;
        for(j = pos; j < pos + rcount; j++){
            printf("%02x ", p[j]);
        }
        printf("   *");
        for(j = pos; j < pos + rcount; j++){
            if(isalpha(p[j]))
                printf("%s ", &p[j]);
            else
                printf(".");
            //print("%s ", p[j]);
        }
        printf("*\n");
        pos += rcount;
    }
}

输出

$ ./a.out 
len=80
this is an array of bytes, of which we will read some of them into our function 74 68 69 73 20    *this is an array of bytes, of which we will read some of them into our function his is an array of bytes, of which we will read some of them into our function is is an array of bytes, of which we will read some of them into our function s is an array of bytes, of which we will read some of them into our function .*
is an array of bytes, of which we will read some of them into our function 69 73 20 61 6e    *is an array of bytes, of which we will read some of them into our function s an array of bytes, of which we will read some of them into our function .an array of bytes, of which we will read some of them into our function n array of bytes, of which we will read some of them into our function *
 array of bytes, of which we will read some of them into our function 20 61 72 72 61    *.array of bytes, of which we will read some of them into our function rray of bytes, of which we will read some of them into our function ray of bytes, of which we will read some of them into our function ay of bytes, of which we will read some of them into our function *
y of bytes, of which we will read some of them into our function 79 20 6f 66 20    *y of bytes, of which we will read some of them into our function .of bytes, of which we will read some of them into our function f bytes, of which we will read some of them into our function .*
bytes, of which we will read some of them into our function 62 79 74 65 73    *bytes, of which we will read some of them into our function ytes, of which we will read some of them into our function tes, of which we will read some of them into our function es, of which we will read some of them into our function s, of which we will read some of them into our function *
, of which we will read some of them into our function 2c 20 6f 66 20    *..of which we will read some of them into our function f which we will read some of them into our function .*
which we will read some of them into our function 77 68 69 63 68    *which we will read some of them into our function hich we will read some of them into our function ich we will read some of them into our function ch we will read some of them into our function h we will read some of them into our function *
 we will read some of them into our function 20 77 65 20 77    *.we will read some of them into our function e will read some of them into our function .will read some of them into our function *
ill read some of them into our function 69 6c 6c 20 72    *ill read some of them into our function ll read some of them into our function l read some of them into our function .read some of them into our function *
ead some of them into our function 65 61 64 20 73    *ead some of them into our function ad some of them into our function d some of them into our function .some of them into our function *
ome of them into our function 6f 6d 65 20 6f    *ome of them into our function me of them into our function e of them into our function .of them into our function *
f them into our function 66 20 74 68 65    *f them into our function .them into our function hem into our function em into our function *
m into our function 6d 20 69 6e 74    *m into our function .into our function nto our function to our function *
o our function 6f 20 6f 75 72    *o our function .our function ur function r function *
 function 20 66 75 6e 63    *.function unction nction ction *
tion 74 69 6f 6e 00    *tion ion on n .*
len=%ld

现在你不再有错误了。我希望这可以帮助您:始终修复所有编译器警告,并且通常会修复您的错误。如果您遇到段错误,并且对此感到困惑,可以使用Valgrind等分析工具进行跟踪。