#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()宏的某处。我猜我的问题究竟是哪里,我将如何解决它?
答案 0 :(得分:1)
cbloc
是指向字符的指针,因此在DFORC
中,ptr
也是指向字符的指针。声明:
printf("'%c', ", *ptr[counter]);
首先使用ptr
作为数组,访问该数组的元素counter
。这会返回char
(不 a char *
)。然后,您尝试取消引用char
,这是没有意义的,因此错误。
要解决此问题,请将该语句更改为以下任一语句:
printf("'%c', ", ptr[counter]);
printf("'%c', ", *(ptr + counter));