静态的声明一个指针变量

2013-01-01 14:50:35 · 作者: · 浏览: 358

    我是从Android的GPS模拟器代码中看到的,觉得很有意思,就记录下来了

    首先是结构体的定义:

    [cpp]

    /* this is the state of our connection to the qemu_gpsd daemon */

    typedef struct {

    int                     init;

    int                     fd;

    GpsCallbacks            callbacks;

    pthread_t               thread;

    int                     control ;

    } GpsState;

    /* this is the state of our connection to the qemu_gpsd daemon */

    typedef struct {

    int                     init;

    int                     fd;

    GpsCallbacks            callbacks;

    pthread_t               thread;

    int                     control ;

    } GpsState;

    然后声明结构体指针变量

    static GpsState  _gps_state ;

    正常使用:

    [cpp]

    static int

    qemu_gps_init(GpsCallbacks* callbacks)

    {

    GpsState*  s = _gps_state;

    if (!s->init)

    gps_state_init(s, callbacks);

    if (s->fd < 0)

    return -1;

    return 0;

    }

    static int

    qemu_gps_init(GpsCallbacks* callbacks)

    {

    GpsState*  s = _gps_state;

    if (!s->init)

    gps_state_init(s, callbacks);

    if (s->fd < 0)

    return -1;

    return 0;

    }