当前位置:编程学习 > C/C++ >>

静态的声明一个指针变量

我是从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[2]; 
} 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[2];
} GpsState;
然后声明结构体指针变量

static GpsState  _gps_state[1];


正常使用:


[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;
}

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,