In C, is it good form to use typedef for a pointer?

前端 未结 8 1688
野性不改
野性不改 2021-01-06 10:46

Consider the following C code:

typedef char * MYCHAR;
MYCHAR x;

My understanding is that the result would be that x is a pointer of type \"

8条回答
  •  不思量自难忘°
    2021-01-06 11:12

    For an API it is not necessary to hide structure definitions and pointers behind "abstract" typedefs.

            /* This is part of the (hypothetical) WDBC- API
            ** It could be found in wdbc_api.h
            ** The struct connection and struct statement ar both incomplete types,
            ** but we are allowed to use pointers to incomplete types, as long as we don't
            ** dereference them.
             */
    struct connection *wdbc_connect (char *connection_string);
    int wdbc_disconnect (struct connection *con);
    int wdbc_prepare (struct connection * con, char *statement);
    
    int main(void)
    {
      struct connection *conn;
      struct statement *stmt;
      int rc;
    
      conn = wdbc_connect( "host='localhost' database='pisbak' username='wild' password='plasser'" );
      stmt = wdbc_prepare (conn, "Select id FROM users where name='wild'" );
    
      rc = wdbc_disconnect (conn);
    
      return 0;
    }
    

    The above fragment compiles fine. (but it fails to link, obviously)

提交回复
热议问题