473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

#define Question

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.
Nov 14 '05 #1
9 2353
Herrcho <he*********@kornet.net> scribbled the following:
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.
thanks in advance.


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"My absolute aspect is probably..."
- Mato Valtonen
Nov 14 '05 #2
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Herrcho <he*********@kornet.net> scribbled the following:
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?


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
Nov 14 '05 #3
nrk
Herrcho wrote:
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.

--
Remove devnull for email
Nov 14 '05 #4
j

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Herrcho <he*********@kornet.net> scribbled the following:
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?


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.''

Martin

Nov 14 '05 #5
On Thu, 5 Feb 2004, j wrote:
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.


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.

Nov 14 '05 #6
On 4 Feb 2004 22:38:49 -0800, in comp.lang.c , he*********@kornet.net
(Herrcho) wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #7
Mark McIntyre <ma**********@spamcop.net> writes:
On 4 Feb 2004 22:38:49 -0800, in comp.lang.c , he*********@kornet.net
(Herrcho) wrote:
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 *


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #8
On Fri, 06 Feb 2004 02:42:06 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On 4 Feb 2004 22:38:49 -0800, in comp.lang.c , he*********@kornet.net
(Herrcho) wrote:
>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 *


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 !
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #9
Mark McIntyre <ma**********@spamcop.net> writes:
On Fri, 06 Feb 2004 02:42:06 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On 4 Feb 2004 22:38:49 -0800, in comp.lang.c , he*********@kornet.net
(Herrcho) wrote:

>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 *


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 !


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kelly | last post by:
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>
3
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
34
by: BQ | last post by:
Hello Is there a way to declare 'FUNCT' via a define so that if its parameter x, a constant, is greater than 35, it returns 56, if not, 20. I would like that at compile time, not at run time. ...
2
by: ooze | last post by:
typedef unsigned long PARAM; typedef PARAM SAP; typedef struct qnode { struct qnode *next; struct qnode *prev; } QNODE;
2
by: Luis Arvayo | last post by:
I am compiling and executing c# code at runtime and I need to define in CompilerParameters.ReferencedAssemblies one of my own assemblies together with the standard System.dll u others. Example:...
11
by: Frank Silvermann | last post by:
#define q p I must admit to a most elementary confusion. I have only ever used #define to, in point of fact, #redefine . The above is standard. Is the following: #define...
17
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
10
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory...
10
by: Zhou Yan | last post by:
I want to define a pointer to a multiple-subscripted array. For instance, I have an array defined as below( I have reduced the size of the array as well as the dimension, but I think it OK to ask...
4
Rabbit
by: Rabbit | last post by:
So I was taking a look at Stromcode's WIN32 API tutorial and he was going over window objects within a parent window. He said you have to assign a resource value to each object. Anyways, I was...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.