指针错误。一元*的类型参数无效

时间:2014-05-21 02:27:32

标签: c pointers memset

#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 个答案:

答案 0 :(得分:1)

cbloc是指向字符的指针,因此在DFORC中,ptr也是指向字符的指针。声明:

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

首先使用ptr作为数组,访问该数组的元素counter。这会返回char a char *)。然后,您尝试取消引用char,这是没有意义的,因此错误。

要解决此问题,请将该语句更改为以下任一语句:

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

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