Recurrsion
Recurrsion

Reputation: 231

Macro function pointer issue

I have been working on writing a few preprocessor macros in C to help me with my work.

            # define    printSTRING(s) printf( # s " has the value");   \
                        for( ; *s != '\0'; s++) \
                        printf(*s); \
                        getch();

I am getting the error: C2105: '++' needs l-value

when I call printSTRING(Payload); where Payload is char Payload[] = "wjdoidnjdeioejneiodejndo";

I take it that its not seeing Payload as a char pointer, but I don't know how to fix the issue.

Upvotes: 0

Views: 375

Answers (2)

Greg Hewgill
Greg Hewgill

Reputation: 994817

That's not they only error you will get. You probably want to use putchar() instead, which takes a single char argument (printf() takes a char * format string, which you're not giving it). Or, you can use puts() which prints the whole string (there's no need to write a loop yourself in that case).

The reason you are getting the error is that Payload is the name of an array, not a pointer. You cannot "increment" an array, although you can use the name of an array as if it were a pointer to the start of the array.

Upvotes: 4

user529758
user529758

Reputation:

  1. You're abusing printf -- that's why the '%s' format specifier is here.
  2. 'Payload' wasn't declared as a char pointer but as a char array -- you can't modify the address of an array. Use simply

    #define printSTRING(s) printf("%s has the value %s", #s, s)
    

instead.

Upvotes: 3

Related Questions