Using define with variable values

M

msigwald

The following line of code works, however, since my professor is a real
purist of c, I would like to know if this code is valid (is it good
code or a piece of crap?):
#define DMP_FILE argv[argc-1]

This would be use to do something like this:
void main(int argc,char *argv[])
{
FILE *p2file=fopen(DMP_FILE,"w");
}

Is this a macro? Cause in the examples of macro I've studied, variables
were involved in the macro itself. This would be like a regular define,
but with variable values.
 
W

Walter Roberson

The following line of code works, however, since my professor is a real
purist of c, I would like to know if this code is valid (is it good
code or a piece of crap?):
#define DMP_FILE argv[argc-1]

It is completely legitimate C. There are two kinds of #define,
one "object like" and the other "function like". Either one is
permitted to include a wide range of syntax, not limited to
constants or variables or simple expressions. It is valid C to
use something like #define BEGIN {
or to otherwise appear to distort the syntax of C.

Whether it is good -style- is a different question.

This would be use to do something like this:
void main(int argc,char *argv[])
{
FILE *p2file=fopen(DMP_FILE,"w");
}

What happens if no arguments were passed? Are you going to
overwrite the binary executable itself?
 
E

Eric Sosman

The following line of code works, however, since my professor is a real
purist of c, I would like to know if this code is valid (is it good
code or a piece of crap?):
#define DMP_FILE argv[argc-1]

This would be use to do something like this:
void main(int argc,char *argv[])
{
FILE *p2file=fopen(DMP_FILE,"w");
}

Is this a macro? Cause in the examples of macro I've studied, variables
were involved in the macro itself. This would be like a regular define,
but with variable values.

The macro definition is valid, and the expansion is
what you intended. However, it's "fragile" because it
relies on the particular names `argc' and `argv' -- and
although these are very often used as the names of the
arguments of main(), other names are permissible:

int main(int zaphod, char **just_this_guy) {
time_t argv = time(NULL);
clock_t argc = clock();
...

is perfectly valid, but would play havoc with your macro.
If you're sure that no one who uses your macro will ever
do anything so stupidly perverse, fine -- but it's usually
best not to "build in" such dependencies. Your macro will
work, but it's probably not Best Practice.

However, your purist professor may not even bother with
the possible shortcomings of the macro, since you've given
him two count them two bona-fide errors to fixate on. Walter
Roberson already pointed out one of them; you may discover
the other by a careful examination of the differences between
your main() and the main() in my reply.
 
M

msigwald

The fopen line was just an example, the actual use of the macro is
diferent.
If no arguments were passed, the actual program will never reach the
dmpfile line.
 
K

Keith Thompson

The fopen line was just an example, the actual use of the macro is
diferent. If no arguments were passed, the actual program will
never reach the dmpfile line.

Just an example of what? Please provide some context when posting a
followup.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Also, go back to your original program and ask yourself what the
main() function returns.
 
P

Peter Shaggy Haywood

Groovy hepcat (e-mail address removed) was jivin' on 29 Apr 2005 21:23:41
-0700 in comp.lang.c.
Using define with variable values's a cool scene! Dig it!
The following line of code works, however, since my professor is a real
purist of c, I would like to know if this code is valid (is it good
code or a piece of crap?):

Piece of crap. (Well, you did ask.)
#define DMP_FILE argv[argc-1]

This would be use to do something like this:
void main(int argc,char *argv[])

It has been stated time and time again in this newsgroup that main()
must return an int, not void. Inspite of us saying this until we're
blue in the face, you still define main() with a return type of void.
Not a good start!
Of course, as well as making main() return an int, you should
actually have it return something. Portable return value for main()
are 0, EXIT_SUCCESS and EXIT_FAILURE (the latter two being macros
defined in stdlib.h).

int main(int argc, char **argv)
{
FILE *p2file=fopen(DMP_FILE,"w");

There are a couple of problems here. Firstly, your macro DMP_FILE
assumes that argc > 0. This may not be always be the case. You
shouldn't take it for granted.
Secondly, you have not included stio.h, and so you don't have a
valid declaration of fopen() in scope.

return 0;
}

Is this a macro?

No. This (the thing in the code directly above) is a function. Its
name is main(). It uses a macro, whose name is DMP_FILE, which is
defined above it.
Cause in the examples of macro I've studied, variables
were involved in the macro itself. This would be like a regular define,
but with variable values.

Macros do not contain variables. However, function-like macros may
have parameters. This allows the user to pass values (from variables
or otherwise) to macros.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Peter Shaggy Haywood

Groovy hepcat Peter "Shaggy" Haywood was jivin' on Sun, 01 May 2005
02:49:17 GMT in comp.lang.c.
Re: Using define with variable values's a cool scene! Dig it!
Groovy hepcat (e-mail address removed) was jivin' on 29 Apr 2005 21:23:41
-0700 in comp.lang.c.
Using define with variable values's a cool scene! Dig it!

Secondly, you have not included stio.h, and so you don't have a
^^^^^^
Sorry! Typo. Should read "stdio.h".

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top