Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #136744

Re: Unwanted, implicit type conversion to a pointer when a value is zero?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: Unwanted, implicit type conversion to a pointer when a value is zero?
Date 2018-10-13 12:54 +0200
Organization A noiseless patient Spider
Message-ID <ppsisu$e0k$1@dont-email.me> (permalink)
References <eaf12c90-58a9-4a8f-b12f-4c0f79a50c59@googlegroups.com>

Show all headers | View raw


On 13/10/18 02:12, jladasky@itu.edu wrote:
> I just encountered a surprising situation.  I hope that I do not omit too much detail in describing it, but I'm trying to avoid spending a lot of time on the embedded system library I'm using, as I believe this is a C language issue and not an issue with the library.
> 
> I have a program with two very similar lines:
> 
>      nrf_drv_twi_tx(&m_twi_master, MAX20303_MAIN_I2C_ADDR, 0x00, 1, true);
> 
> and
> 
>      nrf_drv_twi_tx(&m_twi_master, MAX20303_MAIN_I2C_ADDR, 0x01, 1, true);
> 
> The only difference is the third argument, as you can see.
> 
> I'm compiling with GCC, using very strict error checking.  All warnings are promoted to errors.  The first line compiles successfully.  The second line fails thus:
> 
>      ./main.c:59:59: error: passing argument 3 of 'nrf_drv_twi_tx' makes
>      pointer from integer without a cast [-Werror=int-conversion]
>       nrf_drv_twi_tx(&m_twi_master, MAX20303_MAIN_I2C_ADDR, 0x01, 1, true);
>                                                             ^~~~
>      In file included from ./twi.h:12:0,
>                       from ./main.c:18:
>      /home/.../nrf_drv_twi.h:303:12: note: expected 'const uint8_t * {aka
>      const unsigned char *}' but argument is of type 'int'
>      ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,
>                 ^~~~~~~~~~~~~~
> 
> Now, I know that I've made an error in each case, I need to supply a pointer to a data buffer for that third argument rather than (in my cases a single byte of) the data that I want to send.  However, I am surprised that the first function call does not generate an error if that is true.  GCC was happy to cast 0x00 to a pointer (probably to NULL?).  It only complained when I tried to cast 0x01 to a pointer.  Is this expected?  Desirable?
> 
> Thanks for your insights.
> 


Yes.  The integer constant 0 is a "null pointer" in C.  (Note that this 
is /not/ the same as an int variable that happens to be 0!)  It is 
usable as a pointer of any time, usually to indicate an invalid pointer 
value.

This is far too deeply ingrained in C for there to be a warning about 
passing 0 when a pointer is called for, as it is valid and very commonly 
used.

In modern C++, "nullptr" should be used instead of 0 for null pointers, 
and gcc has a warning "-Wzero-as-null-pointer-constant".  But that is 
not available for C compilation.



Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Unwanted, implicit type conversion to a pointer when a value is zero? jladasky@itu.edu - 2018-10-12 17:12 -0700
  Re: Unwanted, implicit type conversion to a pointer when a value is zero? David Brown <david.brown@hesbynett.no> - 2018-10-13 12:54 +0200
    Re: Unwanted, implicit type conversion to a pointer when a value is zero? Keith Thompson <kst-u@mib.org> - 2018-10-13 11:36 -0700
  Re: Unwanted, implicit type conversion to a pointer when a value is zero? Bart <bc@freeuk.com> - 2018-10-13 15:19 +0100
    Re: Unwanted, implicit type conversion to a pointer when a value is zero? Keith Thompson <kst-u@mib.org> - 2018-10-13 11:37 -0700
      Re: Unwanted, implicit type conversion to a pointer when a value is zero? Bart <bc@freeuk.com> - 2018-10-13 20:21 +0100
        Re: Unwanted, implicit type conversion to a pointer when a value is zero? Keith Thompson <kst-u@mib.org> - 2018-10-13 12:57 -0700

csiph-web