2016-05-10 13 views
0

私はテキスト文字列にメモリダンプを行うCプログラムを持っています。一定量のバイトを印刷し、次の行に移動してもう少し印刷したいと思います。私の2回目のループでは、私がメモリに書き込んでいるのでエラーが出ます。私の第二のループでは、私は私が最初のループで印刷まったく同じ文字を印刷したいのですが、代わり進のASCIIとして:配列インデックスとしてCポインタを2回参照する

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

は、なぜ私がメモリに書き込みを続けるのですか?私は私が知っているポインタを進めていません。

+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 

今、あなたはもうエラーを持っていません。私はこれがあなたを助けることを願っています:常にすべてのコンパイラ警告を修正し、しばしばあなたのエラーを修正します。あなたがsegfaultを持っていて混乱している場合は、Valgrindのような解析ツールでそれを追跡できます。

関連する問題