简体   繁体   中英

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. 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. The statement:

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

First uses ptr as an array, accessing element counter of that array. This returns a char ( not a char * ). You then try to dereference that char , which is doesn't make sense, hence the error.

To fix this, change that statement to either of the following statements:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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