简体   繁体   English

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

[英]Error with pointers. Invalid type argument of unary *

#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");
}

Below is the header I wrote for debugging with pointers 以下是我编写的用于调试指针的标头

#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

The error is happening somewhere in the DFORC() macro. 该错误发生在DFORC()宏中的某处。 I guess my question is where is that exactly and how would I fix it? 我想我的问题是确切的位置以及如何解决?

cbloc is a pointer to characters, so in DFORC , ptr is also a pointer to characters. cbloc是指向字符的指针,因此在DFORCptr也是指向字符的指针。 The statement: 该声明:

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

First uses ptr as an array, accessing element counter of that array. 首先使用ptr作为数组,访问该数组的元素counter This returns a char ( not a char * ). 这将返回一个char不是 char * )。 You then try to dereference that char , which is doesn't make sense, hence the error. 然后,您尝试取消对该char引用,这是没有意义的,因此会出现错误。

To fix this, change that statement to either of the following statements: 要解决此问题,请将该语句更改为以下任一语句:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM