Carramba <> writes:
> sorry... old code..
here is the "right version"
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #define __buff(s, c) s_c
Identifiers starting with underscores are reserved to the
implementation. It's more complex than that, but you should avoid
using such identifiers them in your own code.
This macro ignores its arguments and expands to the identifier "s_c".
I doubt that that's what you intended. You might want to look into
the "##" operator.
You've managed to write code that compiles, but that doesn't do
anything resembling what you're trying to do.
> int main(){
Ok, but "int main(void)" is more explicit.
> int i;
> int __buff(*send, ZERO);
This simply expands to
int s_c;
> for(i=0;i<4;i++){
> int __buff(*send,i);
You declare another variable named "s_c" inside your loop, but you
don't use it.
> }
> for(i=0;i<4;i++){
> __buff(send,i) = malloc(sizeof (__buff(*send,i)));
This expands to
s_c = malloc(sizeof s_c);
Since s_c is an int, and malloc() returns a void* (and the compiler
knows it, since you have the correct "#include <stdlib.h>" directive),
this is illegal (actually a constraint violation; the compiler is
required to issue a diagnostic, but it's allowed to accept it).
More briefly, your compiler almost certainly warned you about a type
mismatch, but you ignored the warning. If your compiler *didn't* warn
you, crank up its warning levels until it does.
> }
>
> for(i=0;i<4;i++){
> __buff(send,i) = i;
s_c = i;
> }
> for(i=0;i<4;i++){
> printf("%d \n",__buff(send,i));
printf("%d \n",s_c);
> }
>
> return 0;
> }
I can really only guess what you're *trying* to do. Looking at a
snippet of your code:
#define __buff(s, c) s_c
/* ... */
for(i = 0; i < 4; i++) {
int __buff(*send,i);
}
I *think* you're trying to create the following declarations:
int *send_0;
int *send_1;
int *send_2;
int *send_3;
The preprocessor just doesn't work that way. If your __buff() macro
were defined correctly, using the ## operator, the line would expand
to
int *send_i;
Macro expansion does textual substitution. The *value* of i is not
available to the preprocessor; it doesn't exist until your program is
actually running.
And even if that worked, the scope of the variable declaration is just
the compound statement. A new variable is created on each iteration
of the loop, and destroyed on reaching the closing '}'.
Basically, you've badly mixed up compilation time and execution time.
You're trying to create a numbered sequence of variables.
That's called an array:
int *send[4];
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"