For the sake of readability, I've been trying to do something like this:

#define initialize(x)(type x = initfunction())
int main(){
    initialize(example);
    example.item = 0;
    return 0;
}

where type is a typedef'd struct, and initfunction() generates one, inits values, and returns it. My problem is, I get these errors:

warning: unused variable 'example'
error: 'example' undeclared (first use in this function)

-Andrew

For the sake of readability

I don't see how this will make your code more readable.
It will only introduce complexity, and I think it won't be useful anyway, because the initialize(x) macro will only work if no statement has been executed in the function it is "called" from. The preprocessor will replace each "call" to a macro, with the macro's "body". That is, after preprocessing, your code will look like this:

int main() {
  (type example = initfunction()); /* notice the parentheses! */
  example.item = 0;
  return 0;
}

See now why it doesn't compile?
If you want your code to compile, then you should change your intitialize(x) macro definition to: #define initialize(x) type x = initfunction() .
But keep in mind that I'm not for such programming practices, it obscures your code and introduces errors.

[EDIT]
When strictly adhering to the C89 standard, this won't even compile:

#define initialize(x) type x = initfunction()

int main() {
  initialize(example);
  example.item = 0;
  initialize(temp);
  return 0;
}

Why? Because the standard forbids it.
[/EDIT]

Edited 6 Years Ago by mvmalderen: n/a

well, that explains it pretty well. Thanks. The reason I was doing that, is that some of the "type" variables are static, and others are external. Since I wanted both to look more or less the same in the code, I wanted the macros to handle the differences. Something like:

#define initializeExtern(x) extern type x; x = initfunction()
#define initializeStatic(x) type x = initfunction()

How do you suppose I should handle something like this?

Thanks,
Andrew

How do you suppose I should handle something like this?

Create an initialize() function which takes a pointer to a 'type' as argument, and each time you want to initialize a variable of type 'type', then you call that function and pass it the address of the 'type' variable you wish to initialize.

void initialize( type *i )
{
  /* initialization code */
}

Wouldn't it be simpler to do like this ?

#define TYPE extern type
// then in code it would be 
type x = initfunction()
// or
TYPE x = initfunction()

Both look more or less the same AND you will know that if type declaration is done with macro - it will be extern.

@tux4life: yeah... I know now that that's the way I should do it but... so many lines to change D: better get to it I guess.

@0x69: that doesn't really work, because declaring an extern and defining in the same line is not allowed.

Edit: a few minutes (and segfaults) later, everything's working out the way tux4life suggested. Thanks :)

Edited 6 Years Ago by AndrewYY: n/a

@AndrewYY
It may depend on compiler which you are using. For example - following code compiles without any problem on GCC 4.2:

#define INT extern int

int main () {
	INT a;
    return 0;
}

Edited 6 Years Ago by 0x69: n/a

This question has already been answered. Start a new discussion instead.