#define Question

H

Herrcho

The following code is from Exercise 9.3 from 'A book on C'

//****************************************************
#include <stdio.h>
#define string char *


int main()
{
string a[] = {"I", "like", "to", "fight,"},
b[] = {"pinch,", "and", "bight."};

printf("%s %s %s %s %s %s %s\n", a[0], a[1], a[2], a[3], b[0], b[1], b[2]);

return 0;
}

//******************************************************

i don't know why the above doesn't run.
the Question is what single character do i need to add to make it works with
#define.

thanks in advance.
 
J

Joona I Palaste

Herrcho said:
The following code is from Exercise 9.3 from 'A book on C'
//****************************************************
#include <stdio.h>
#define string char *

Macros should usually be in all uppercase.

This is actually a question for the C gurus out there: does the "str
identifiers are reserved" rule apply to macros?
int main()
{
string a[] = {"I", "like", "to", "fight,"},
b[] = {"pinch,", "and", "bight."};

Here's your problem. Think about what the above definition expands
to when you expand the macro "string" to "char *".
printf("%s %s %s %s %s %s %s\n", a[0], a[1], a[2], a[3], b[0], b[1], b[2]);
return 0;
}

i don't know why the above doesn't run.
the Question is what single character do i need to add to make it works with
#define.

Did the book tell you you need to add a single character? I'll give
you a hint. You've typed plenty of that character above.
 
M

Martin Dickopp

Joona I Palaste said:
Macros should usually be in all uppercase.

This is actually a question for the C gurus out there: does the "str
identifiers are reserved" rule apply to macros?

Only if one of the headers <stdlib.h> or <string.h> has been
included. However, I'd consider it wise to avoid defining such
macros even if neither of these headers is used.

Martin
 
N

nrk

Herrcho said:
The following code is from Exercise 9.3 from 'A book on C'

//****************************************************
#include <stdio.h>
#define string char *


int main()
{
string a[] = {"I", "like", "to", "fight,"},
b[] = {"pinch,", "and", "bight."};

printf("%s %s %s %s %s %s %s\n", a[0], a[1], a[2], a[3], b[0], b[1],
b[2]);

return 0;
}

//******************************************************

i don't know why the above doesn't run.
the Question is what single character do i need to add to make it works
with
#define.

thanks in advance.

This exercise shows spectacularly why it is a braindead idea to write
declarations like:
char* a, b;

Sing along with me:
"Twinkle Twinkle little star, How I wonder where you are..."

-nrk.
 
J

Jarno A Wuolijoki

c89:
``4.13 FUTURE LIBRARY DIRECTIONS
The following names are grouped under individual headers for
convenience. All external names described below are reserved no
matter what headers are included by the program.''

This is one of the issues that get beaten to death every month
or two here. #defines are not external names any more than local
variables are.
 
J

j

Martin Dickopp said:
Only if one of the headers <stdlib.h> or <string.h> has been
included. However, I'd consider it wise to avoid defining such
macros even if neither of these headers is used.

c89:
``4.13 FUTURE LIBRARY DIRECTIONS
The following names are grouped under individual headers for
convenience. All external names described below are reserved no
matter what headers are included by the program.''

c99:
``7.26 Future library directions
The following names are grouped under individual headers for
convenience. All external names described below are reserved no
matter what headers are included by the program.''
 
M

Mark McIntyre

The following code is from Exercise 9.3 from 'A book on C'

if this is really from that book, throw the book away....
#define string char *
(snip)

string a[] = {"I", "like", "to", "fight,"}, b[] = {"pinch,", "and", "bight."};

This is why its generally a BAD IDEA to use macros to hide types,
especially pointer types.

Macros are quite literally replaced in the text before compilation. so
every occurence of "string" is replaced by "char*". If you perform
that substitution in the code above, you will see the problem.
 
K

Keith Thompson

Mark McIntyre said:
if this is really from that book, throw the book away....

Actually, if the point of the exercise is to demonstrate *why* such a
macro is a bad idea, it's probably a pretty good exercise.
 
M

Mark McIntyre

Actually, if the point of the exercise is to demonstrate *why* such a
macro is a bad idea, it's probably a pretty good exercise.

only if the exercise is designed to see if you understand your
compiler error messages..... the code doesn't compile !
 
K

Keith Thompson

Mark McIntyre said:
only if the exercise is designed to see if you understand your
compiler error messages..... the code doesn't compile !

You're right, it doesn't. I don't think that contradicts my point.
I suspect the point of the exercise is for the reader to figure out
*why* the code doesn't compile -- ideally without having to compile it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top