-3

From my understanding, preprocessor directives are used to modify source code before its compiled. I'm writing some code of an assignment, but my macro causes errors. In my assignment I have to write several files with main() with identical variables. I want to use macros like this.

#define SETUP (i)\
     char* memory = (char*) malloc(sizeof(char)*(i));\
     char* memory2 = (char*) malloc(sizeof(char)*(i))
#define CLOSE ()\
     free(memory);\
     free(memory2)

int main () {
     int i = 7;
     SETUP(i);

/****************************/
Do stuff with memory and memory2
/*****************************/

     CLOSE();
}

this way I can make changes to the beginning and end of each file's main() without having to change each one. Can someone tell me what I don't understand?

yogurt
  • 1
  • 1
  • `but my macro causes errors`..what are those? – Sourav Ghosh Nov 29 '19 at 10:11
  • Welcome to Stack Overflow! In your `main` function, do you have a declaration for `i` ***before*** the invocation of `SETUP(i)`? – Adrian Mole Nov 29 '19 at 10:12
  • 1
    `(char*) memory = ...` looks pretty invalid. Don't really remember if such a declarator is valid, but I'd start with that. – StoryTeller - Unslander Monica Nov 29 '19 at 10:14
  • sorry, I fixed my post – yogurt Nov 29 '19 at 10:14
  • The macros themselves are ok but you never declare variables `memory` and `memory2` anywhere. Start by writing the correct code without any macros. – Lundin Nov 29 '19 at 10:15
  • `(char *) memory` is not an _lvalue_ so cannot be assigned to. Perhaps you meant `char * memory`? – Ian Abbott Nov 29 '19 at 10:20
  • You keep editing the code, which invalidates the comments and answers. Is it working now? – Ian Abbott Nov 29 '19 at 10:24
  • no, it's not. The code I wrote was just an example, I added my actual code – yogurt Nov 29 '19 at 10:27
  • One other thing - if your code uses `malloc()` and `free()`, it is necessary to `#include ` - without that, the casts on `malloc()` mask a programming error. Then, unless your C compiler is actually a C++ compiler, the `(char *)` casts of `malloc()` are not required. – Peter Nov 29 '19 at 11:12
  • I recommend that you read and understand [the question on why not to cast the return value of `malloc()` and family in C](/q/605845). And note that `sizeof` is specified in units of `char`, so `sizeof (char)` must necessarily be 1, making that a redundant multiplication. Also, look into the `do {...} while (0)` idiom for multi-statement macros - that may well save your bacon when you put cleanup in the error-handling). – Toby Speight Nov 29 '19 at 14:32

2 Answers2

4

In the macro definitions, you have added a space between the macro name and opening parenthesis:

#define SETUP (args_needed)\

That defines an ordinary macro rather than a macro that takes parameters. Therefore args_needed is not a macro parameter and is not defined.

Remove the space between the macro name and opening parenthesis:

#define SETUP(args_needed)\

It is OK to insert a space when invoking the macro:

SETUP (10);

but that is considered poor coding style by many.

Ian Abbott
  • 15,083
  • 19
  • 33
0

You didn't declare i anywhere. SETUP(i); just passes i for the macro parameter i, so the code that the macro expands to contains an undeclared i. You probably want something like SETUP(10); instead.

Also this here

(char*) memory

Isn't how you declare a variable. This is a typecast, and it fails because memory is undefined. Remove the parentheses.

Blaze
  • 16,736
  • 2
  • 25
  • 44