Is this a good idea? 
Author Message
 Is this a good idea?

Hi all,

I am wondering if the following code is OK. Well,
it compiles and runs OK. What I am worrying about
is the assignment from a macro to a. Is that OK?

/////////////////////////////////////
#include <stdio.h>

#define DEFAULT_NAME "hello, there!"

int main()
{
        char *a;
        a = DEFAULT_NAME;
        /*^^^^^^^^^^^^^^*/

        (void)puts(a);
        return 0;

Quote:
}



Sun, 09 Nov 2003 23:15:34 GMT  
 Is this a good idea?

Quote:

>  What I am worrying about
>  is the assignment from a macro to a. Is that OK?

>  /////////////////////////////////////
>  #include <stdio.h>

>  #define DEFAULT_NAME "hello, there!"

>  int main()
>  {
>    char *a;
>    a = DEFAULT_NAME;
>    /*^^^^^^^^^^^^^^*/

>    (void)puts(a);
>    return 0;
>  }

Yes, the assignment is OK, but note that the string literal a points
at is unmodifiable; trying to write to it would invoke undefined
behavior.

Gergo

--
A quarrel is quickly settled when deserted by one party; there is no
battle unless there be two.  -- Seneca



Sun, 09 Nov 2003 23:25:30 GMT  
 Is this a good idea?

Quote:

> I am wondering if the following code is OK. Well,
> it compiles and runs OK. What I am worrying about
> is the assignment from a macro to a. Is that OK?

Yes, it is; that's because there is really no such assignment. A macro
is not an object, it is a call for simple text substitution.

Quote:
> #include <stdio.h>

> #define DEFAULT_NAME "hello, there!"

> int main()
> {
>    char *a;
>    a = DEFAULT_NAME;

Ok. This line gets preprocessed into

  a = "hello, there!";

and is then treated _exactly_ as if you typed that literal line into the
text. By the time the actual translation from text to object code is
done, all references to macros have been translated into literal text.

Quote:
>    (void)puts(a);

There is no good reason whatsoever for that cast, btw.

Richard



Sun, 09 Nov 2003 23:24:31 GMT  
 Is this a good idea?

Quote:
> Hi all,
> I am wondering if the following code is OK. Well,
> it compiles and runs OK. What I am worrying about
> is the assignment from a macro to a. Is that OK?
> /////////////////////////////////////
> #include <stdio.h>
> #define DEFAULT_NAME "hello, there!"
> int main()
> {
>    char *a;
>    a = DEFAULT_NAME;
>    /*^^^^^^^^^^^^^^*/
>    (void)puts(a);
>    return 0;
> }

Yes, this is perfectly OK. The C compiler never even sees the macro.
As far as the compiler is concerned, it thinks you actually typed:
a = "hello, there!";
There is nothing dangerous in using macros this way.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"You could take his life and..."
   - Mirja Tolsa



Sun, 09 Nov 2003 23:27:03 GMT  
 Is this a good idea?

Quote:


> >       (void)puts(a);
> There is no good reason whatsoever for that cast, btw.

I understand that it can silence warnings from some compilers
(but not ones that I use).
--
"I should killfile you where you stand, worthless human." --Kaz


Sun, 09 Nov 2003 23:32:48 GMT  
 Is this a good idea?

Quote:


>> >   (void)puts(a);
>> There is no good reason whatsoever for that cast, btw.

>I understand that it can silence warnings from some compilers
>(but not ones that I use).

lint is the one which is silenced by this silly cast.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 10 Nov 2003 00:18:27 GMT  
 Is this a good idea?

Quote:

>> Hi all,

>> I am wondering if the following code is OK. Well,
>> it compiles and runs OK. What I am worrying about
>> is the assignment from a macro to a. Is that OK?

>> /////////////////////////////////////
>> #include <stdio.h>

>> #define DEFAULT_NAME "hello, there!"

>> int main()
>> {
>>        char *a;
>>        a = DEFAULT_NAME;
>>        /*^^^^^^^^^^^^^^*/

>>        (void)puts(a);
>>        return 0;
>> }

>Yes, this is perfectly OK. The C compiler never even sees the macro.
>As far as the compiler is concerned, it thinks you actually typed:
>a = "hello, there!";

The preprocessor is, conceptually (and, in some cases, physically)
part of the compiler.

Quote:
>There is nothing dangerous in using macros this way.

Indeed.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 10 Nov 2003 00:17:24 GMT  
 Is this a good idea?

Quote:
> > #include <stdio.h>

> > #define DEFAULT_NAME "hello, there!"

> > int main()
> > {
> > char *a;
> > a = DEFAULT_NAME;
> > /*^^^^^^^^^^^^^^*/

> > (void)puts(a);
> > return 0;
> > }

> There is nothing dangerous in using macros this way.

But there is something dangerous about assigning string literals to pointers.

Bart.



Mon, 10 Nov 2003 01:09:47 GMT  
 Is this a good idea?

Quote:



> > >     (void)puts(a);
> > There is no good reason whatsoever for that cast, btw.

> I understand that it can silence warnings from some compilers
> (but not ones that I use).

It will quiet "return value not used" messages from lint.
Actually it is quite useful, in that someone revising the code
later will realize there is info available at that point.  Maybe
it is in an embedded system, and puts can detect that the display
has been disconnected and return an error.  Detecting that lets
the programmer decide to resort to a noise generator.  I think
most people don't realize that puts returns an error condition.

--

http://www.qwikpages.com/backstreets/cbfalconer :=(down for now)
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Mon, 10 Nov 2003 01:16:05 GMT  
 Is this a good idea?


Quote:
>> > #include <stdio.h>

>> > #define DEFAULT_NAME "hello, there!"

>> > int main()
>> > {
>> > char *a;
>> > a = DEFAULT_NAME;
>> > /*^^^^^^^^^^^^^^*/

>> > (void)puts(a);
>> > return 0;
>> > }

>> There is nothing dangerous in using macros this way.

>But there is something dangerous about assigning string literals to pointers.

Only when the pointers aren't const-qualified.

--



Mon, 10 Nov 2003 01:15:06 GMT  
 Is this a good idea?

Quote:
> > > >     (void)puts(a);
> > > There is no good reason whatsoever for that cast, btw.

> > I understand that it can silence warnings from some compilers
> > (but not ones that I use).

> It will quiet "return value not used" messages from lint.
> Actually it is quite useful, in that someone revising the code
> later will realize there is info available at that point.  Maybe
> it is in an embedded system, and puts can detect that the display
> has been disconnected and return an error.  Detecting that lets
> the programmer decide to resort to a noise generator.  I think
> most people don't realize that puts returns an error condition.

But unnecessary casts:

1- Make the code ugly.
2- Can hide mistakes (malloc() being the classic example).

I cast only when the compiler complains and I know that the
conversion is safe.

Bart.



Mon, 10 Nov 2003 05:01:57 GMT  
 Is this a good idea?

Quote:




> >> >    (void)puts(a);
> >> There is no good reason whatsoever for that cast, btw.

> >I understand that it can silence warnings from some compilers
> >(but not ones that I use).

> lint is the one which is silenced by this silly cast.

A good lint utility, like PC-lint, can be configured to inhibit warnings
about unused returned values, on a per-function basis. An example from a
PC-lint options file:

-esym(534,strcat,strcpy,printf,fprintf,puts)

Regards
--
Jetson



Mon, 10 Nov 2003 15:37:48 GMT  
 Is this a good idea?

Quote:

> > > > >     (void)puts(a);
> > > > There is no good reason whatsoever for that cast, btw.

> > > I understand that it can silence warnings from some compilers
> > > (but not ones that I use).

> > It will quiet "return value not used" messages from lint.
> > Actually it is quite useful, in that someone revising the code
> > later will realize there is info available at that point.  Maybe
> > it is in an embedded system, and puts can detect that the display
> > has been disconnected and return an error.  Detecting that lets
> > the programmer decide to resort to a noise generator.  I think
> > most people don't realize that puts returns an error condition.

> But unnecessary casts:

> 1- Make the code ugly.
> 2- Can hide mistakes (malloc() being the classic example).

> I cast only when the compiler complains and I know that the
> conversion is safe.

In this case it is not really a cast in spite of appearances.  It
simply says "take this result and discard it".  If you prefer you
can write "junk = puts(a);" to silence lint, at a slight loss of
efficiency.

--

http://www.qwikpages.com/backstreets/cbfalconer :=(down for now)
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Mon, 10 Nov 2003 15:08:39 GMT  
 Is this a good idea?

Quote:
>Hi all,

>I am wondering if the following code is OK. Well,
>it compiles and runs OK. What I am worrying about
>is the assignment from a macro to a. Is that OK?

Yes. It's not an assignment, because macros are only strings
replacements. There are processed by the preprocessor, not by the
compiler.

Quote:
>/////////////////////////////////////
>#include <stdio.h>

>#define DEFAULT_NAME "hello, there!"

>int main()
>{
> char *a;
> a = DEFAULT_NAME;
> /*^^^^^^^^^^^^^^*/

So, this code expands to

 a = "hello, there!";

which is absolutely legal. (It's a pointer assignment).
Note that a pointer to a string literal should be declared pointing to
const:

 char const *a;
 a = DEFAULT_NAME;

Note also that when possible, it is a good idea to declare and
initialize the data on one line:

 char const *a = DEFAULT_NAME;

And in most cases, you want a constant pointer:

 char const *const a = DEFAULT_NAME;

(Maybe not here, because it sounds to be a "default name"... subject
to future change)

Quote:
> (void)puts(a);

You may have a nitpicky lint. This (void) is nothing but useless.

Quote:
> return 0;
>}

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc


Mon, 10 Nov 2003 16:23:04 GMT  
 Is this a good idea?

Quote:
><...> If you prefer you
>can write "junk = puts(a);" to silence lint, at a slight loss of
>efficiency.

and you will probably have an "unused variable" warning!

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Mon, 10 Nov 2003 16:35:52 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. how good am I? Am I Good Enough????

2. Is this a good Idea??

3. Sorting multi column ListView -code-Any better ideas?

4. link libraries to libraries a good idea?

5. Help with ideas, best ways etc...

6. Vivianne has a good idea

7. Is this a good idea? (cont)

8. Bad trick or good idea?

9. Good program ideas?

10. Is this a good Idea??

11. Is this a good Idea??

 

 
Powered by phpBB® Forum Software