2014-05-21 51 views
0
#include "cdebug.h" 
#include "stdlib.h" 

int main() 
{ 
    char *cbloc = (char *)malloc(sizeof(char) * 40); 
    memset(cbloc, 40, sizeof(char) * 40); 
    DFORC(cbloc, 0, sizeof(char) * 40); 
    system("PAUSE"); 
} 

下面無效的類型參數是我調試的指針指針錯誤。的一元*

#ifndef _CDEBUG_H_ 
#define _CDEBUG_H_ 
#include "stdio.h" 

int counter; 

//Debugging functions written by skrillac 

//constants 
#define NEWLN() printf("\n"); 

#define DFORC(ptr, offset, len) for (counter = offset; counter < len+offset; counter++)printf("'%c', ", *ptr[counter]); 
#define DFORI(ptr, offset, len) for (counter = offset; counter < len+offset; counter++)printf("'%i', ", *ptr[counter]); 
#define DFORV(ptr, offset, len) for (counter = offset; counter < len+offset; counter++)printf("%x, ", *ptr[counter]); 

#endif 

的誤差在DFORC()宏某處發生寫頭。我想我的問題是在哪裏,我該如何解決它?

回答

1

cbloc是一個指向字符的指針,所以在DFORCptr也是一個指向字符的指針。語句:

printf("'%c', ", *ptr[counter]); 

首先使用ptr作爲陣列,訪問該陣列的元件counter。這返回char而不是 a char *)。然後,您嘗試解除引用char,這是沒有意義的,因此是錯誤。

爲了解決這個問題,該語句更改爲下面的語句:

printf("'%c', ", ptr[counter]); 

printf("'%c', ", *(ptr + counter));