str != (char *) NULL 
Author Message
 str != (char *) NULL

I read this code (gtk+, programming in c, s.logan):

void somefunction( void )
{
        char *str;

        ...
        if ( str != (char *) NULL )
                ...

Quote:
}

Now, is there any situation possible where leaving the typecast in this
example will lead to an application-error or compilerwarning? I cannot
imagine this would ever lead to an error, but I don't like ignoring the
experience and expertise of people that are so much more experienced
than I am.

Erik



Tue, 08 Jun 2004 22:20:16 GMT  
 str != (char *) NULL

Quote:
> I read this code (gtk+, programming in c, s.logan):

> void somefunction( void )
> {
>      char *str;

>      ...
>      if ( str != (char *) NULL )
>           ...
> }

> Now, is there any situation possible where leaving the typecast in this
> example will lead to an application-error or compilerwarning? I cannot
> imagine this would ever lead to an error, but I don't like ignoring the
> experience and expertise of people that are so much more experienced
> than I am.

I agree with you, I'd write it like this and never think about it again...

char *pStr;
...
if (pStr)
{
    ....

Quote:
}

This is a very common idiom.

--
- Mark A. Odell
- Embedded Firmware Design, Inc.
- http://www.embeddedfw.com



Tue, 08 Jun 2004 22:22:55 GMT  
 str != (char *) NULL


Quote:

> > I read this code (gtk+, programming in c, s.logan):

> > void somefunction( void )
> > {
> >      char *str;

> >      ...
> >      if ( str != (char *) NULL )
> >           ...
> > }

> > Now, is there any situation possible where leaving the typecast in this
> > example will lead to an application-error or compilerwarning? I cannot
> > imagine this would ever lead to an error, but I don't like ignoring the
> > experience and expertise of people that are so much more experienced
> > than I am.

> I agree with you, I'd write it like this and never think about it again...

> char *pStr;
> ...
> if (pStr)
> {
>     ....
> }

> This is a very common idiom.

> --
> - Mark A. Odell
> - Embedded Firmware Design, Inc.
> - http://www.embeddedfw.com

I have to agree with this.  I guess that the complication is testing against
NULL is touted as a good thing in Microsoft views.  For example McConnell
(in Code Complete) advises "while ( BufferPtr != NULL ) ...", although he
also points out that "this one goes against the established C convention".

Bill



Tue, 08 Jun 2004 23:43:04 GMT  
 str != (char *) NULL

Quote:

> void somefunction( void )
> {
>    char *str;

>    ...
>    if ( str != (char *) NULL )
>            ...
> }

> Now, is there any situation possible where leaving the typecast in this
> example will lead to an application-error or compilerwarning?

Yes: programmer abuse. IOW, if the compiler complains when you leave out
the cast, you are doing something else wrong, for example accidentally
making str a char instead of a char *, or using a C++ compiler to
compile C code (yeck!).
Leaving out the cast is always correct when comparing NULL to a pointer.
In fact, it is my stated opinion that putting it in leads to sloppy
programmers. The above code, in C, is much better off without the cast
than with it.

Richard



Tue, 08 Jun 2004 23:46:15 GMT  
 str != (char *) NULL

Quote:

> I have to agree with this.  I guess that the complication is testing against
> NULL is touted as a good thing in Microsoft views.  For example McConnell
> (in Code Complete) advises "while ( BufferPtr != NULL ) ...", although he
> also points out that "this one goes against the established C convention".

Actually, that is simply untrue. while (bufferptr!=NULL) ... is
_identical_ to while (bufferptr) ... . It must be; it is guaranteed to
be; the ISO C Standard demands that it be identical. Neither is more or
less correct than the other

As for convention, convention is what one has encountered so far.

Richard



Tue, 08 Jun 2004 23:49:54 GMT  
 str != (char *) NULL

Quote:
>void somefunction( void )
>{
>    char *str;

>    ...
>    if ( str != (char *) NULL )
>            ...
>}

>Now, is there any situation possible where leaving the typecast in this
>example will lead to an application-error or compilerwarning? I cannot

A compiler is allowed to generate a warning for any reason (even
stupid ones, and outright lies).

This includes putting a warning "casts are ugly" on *ALL* casts,
and the cast above is just begging for an "unnecessary cast" warning.

Quote:
>imagine this would ever lead to an error, but I don't like ignoring the
>experience and expertise of people that are so much more experienced
>than I am.

                                        Gordon L. Burditt


Wed, 09 Jun 2004 00:32:20 GMT  
 str != (char *) NULL

Quote:

> void somefunction( void )
> {
> char *str;
> ...
> if ( str != (char *) NULL )
> ...
> }

Others have already provided the answer, but I would like to misuse
your post to ask my own question.

I have seen a code like this:

  some_type * a;
  other_type * b;
  ...
  b = (other_type *)(void *)a;

Now, in this case, casting a to the type of b was OK, that is not
the problem. I also know that one of the casts is unnecessary.
My question is:

_In_this_case_, are expressions
  b = (other_type *)a;
and
  b = (void *)a;
equivalent?

Peter



Wed, 09 Jun 2004 03:55:10 GMT  
 str != (char *) NULL

Quote:

[...]
> Others have already provided the answer, but I would
> like to misuse your post to ask my own question.

> I have seen a code like this:

>   some_type * a;
>   other_type * b;
>   ...
>   b = (other_type *)(void *)a;

> Now, in this case, casting a to the type of b was OK,
> that is not the problem. I also know that one of the
> casts is unnecessary.

> My question is:

> _In_this_case_, are expressions
>   b = (other_type *)a;
> and
>   b = (void *)a;
> equivalent?

AFAIK, yes. A void * can be assigned any other pointer without an
explicit cast.


Wed, 09 Jun 2004 04:11:04 GMT  
 str != (char *) NULL

Quote:

> > I have to agree with this.  I guess that the complication is testing
against
> > NULL is touted as a good thing in Microsoft views.  For example
McConnell
> > (in Code Complete) advises "while ( BufferPtr != NULL ) ...", although
he
> > also points out that "this one goes against the established C
convention".

> Actually, that is simply untrue.

What is untrue?  That I agree?  That testing against NULL is touted as a
good thing?  That the example exists in McConnell?  That he points out ...?

 while (bufferptr!=NULL) ... is

Quote:
> _identical_ to while (bufferptr) ... . It must be; it is guaranteed to
> be; the ISO C Standard demands that it be identical. Neither is more or
> less correct than the other

Yes, I agree.

Quote:

> As for convention, convention is what one has encountered so far.

No, convention is what is out there to be encountered.  The convention
exists before it is observed.  (Let's not get too bogged down with
philosophy though).  The problem is that there are different conventions in
different environments.  From my recent experience of working in Linux code
and Windows-based code I notice a difference in styles; in Linux code I
would expect not to
see the "== 0" whereas in Windows-based code I would expect to see it.

Quote:

> Richard

Bill


Wed, 09 Jun 2004 04:42:00 GMT  
 str != (char *) NULL

Quote:


> > My question is:

> > _In_this_case_, are expressions
> >   b = (other_type *)a;
> > and
> >   b = (void *)a;
> > equivalent?

> AFAIK, yes. A void * can be assigned any other pointer without an
> explicit cast.

Hmm.. any pointer can be converted to or from void *. That wasn't the
question
however.

The "(other_type *)a" doesn't need to be correctly aligned, else
undefined behaviour. "other_type" need to have less or equally strict
alignment as "some_type".

--
Tor <torust AT online DOT no>



Wed, 09 Jun 2004 06:26:11 GMT  
 str != (char *) NULL

Quote:

> The "(other_type *)a" doesn't need to be correctly aligned, else

Grrr..

The "(other_type *)a" doesn't need to be correctly aligned, i.e.

<gd&r>

--
Tor <torust AT online DOT no>



Wed, 09 Jun 2004 06:59:14 GMT  
 str != (char *) NULL

Quote:



> > > My question is:

> > > _In_this_case_, are expressions
> > >   b = (other_type *)a;
> > > and
> > >   b = (void *)a;
> > > equivalent?

> > AFAIK, yes. A void * can be assigned any other pointer without an
> > explicit cast.

> Hmm.. any pointer can be converted to or from void *. That wasn't the
> question
> however.

Except function pointers.

Micah



Wed, 09 Jun 2004 08:13:30 GMT  
 str != (char *) NULL

Quote:




> [...]
> > Others have already provided the answer, but I would
> > like to misuse your post to ask my own question.

> > I have seen a code like this:

> >   some_type * a;
> >   other_type * b;
> >   ...
> >   b = (other_type *)(void *)a;

> > Now, in this case, casting a to the type of b was OK,
> > that is not the problem. I also know that one of the
> > casts is unnecessary.

> > My question is:

> > _In_this_case_, are expressions
> >   b = (other_type *)a;
> > and
> >   b = (void *)a;
> > equivalent?

> AFAIK, yes. A void * can be assigned any other pointer without an
> explicit cast.

However the result may not necessarily be useful.  If other_type
is unsigned char, this will always work.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Wed, 09 Jun 2004 08:38:27 GMT  
 str != (char *) NULL

Quote:




> > > > My question is:

> > > > _In_this_case_, are expressions
> > > >   b = (other_type *)a;
> > > > and
> > > >   b = (void *)a;
> > > > equivalent?

> > > AFAIK, yes. A void * can be assigned any other pointer without an
> > > explicit cast.

> > Hmm.. any pointer can be converted to or from void *. That wasn't the
> > question
> > however.

> Except function pointers.

I've worked with NULL terminated arrays of pointers to functions.

--
 pete



Wed, 09 Jun 2004 17:24:49 GMT  
 str != (char *) NULL

Quote:





>> > > > My question is:

>> > > > _In_this_case_, are expressions
>> > > >   b = (other_type *)a;
>> > > > and
>> > > >   b = (void *)a;
>> > > > equivalent?

>> > > AFAIK, yes. A void * can be assigned any other pointer without an
>> > > explicit cast.

>> > Hmm.. any pointer can be converted to or from void *. That wasn't the
>> > question
>> > however.

>> Except function pointers.

>I've worked with NULL terminated arrays of pointers to functions.

That doesn't change the fact that you cannot convert implicitly between
function pointers and void *. You can convert a null pointer constant
to a function pointer implicitly, which creates an exception to the
first rule because, for example, (void *)0 is a null pointer constant
so

void (*fp)(void) = (void *)0;

is valid. However

int i = 0;
void (*fp)(void) = (void *)i;

violates a constraint.

--
-----------------------------------------


-----------------------------------------



Wed, 09 Jun 2004 21:48:48 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. char **str vs. char *str[]

2. char *str vs. char str[]

3. char *str vs. char s

4. char* and char str[2]

5. c standard regarding char str[ ], str[50] & *str

6. str* functions and NULL pointers

7. Basic Question - char *str V/S int *

8. register char str[LEN]

9. how to address char * str[] ?

10. char *str - Pointer usage

11. q about array of char str

12. _T(str) vs (CString)str?

 

 
Powered by phpBB® Forum Software