Bryan C.
Bryan C.

Reputation: 117

Referencing a C pointer as array index twice

I have a C program in which I am trying to do a memory dump on a string of text. I want to print a certain amount of bytes and then go down to the next line and print some more. In my second for loop I am getting an error because I am writing on memory. In my second loop I want to print the exact same characters that I printed in the first loop, but as ascii instead of 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;
    }
}

Why do I keep writing on memory? I am not advancing the pointer that I know of.

Upvotes: 0

Views: 62

Answers (1)

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26642

It's important that the program compiles without warning and we got warning from your code:

$ 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]);

We should make sure that the types match when we call functions. If I fix your errors, it looks like your program is fine and there is no error message.

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;
    }
}

Output

$ ./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

Now you don't have an error anymore. I hope this can help you: Always fix all compiler warning and often that will fix your errors. If you have a segfault and you are confused about it, you can track it with analysis tool like Valgrind.

Upvotes: 2

Related Questions