Kelly wrote:
Hi,
Ashamed to ask basic question but I cannot get this code to run,
#define Y one
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void) {
//Y=malloc(sizeof(char *));
printf ("%s",Y);
return 0;
}
I can compile but not run, gives me "the memory cannot be read".
All I want is to printf Y.
Please help, I know this is easy for most of you.
It's easy for anyone who checks the FAQ before posting.
Below is a version of your program which shows various behaviors that you
might one to check. The question and answer in the FAQ can be found at
<http://www.eskimo.com/~scs/C-faq/q11.17.html>
#define Y one
#define Z "one"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STR_aux(x) #x
#define STR(x) STR_aux(x)
int main(void)
{
printf("%s\n", STR_aux(Y));
printf("%s\n", STR(Y));
printf("%s\n", STR_aux(Z));
printf("%s\n", STR(Z));
printf("%s\n", Z);
return 0;
}
[output]
Y
one
Z
"one"
one
--
Martin Ambuhl