鑑於這種結構:如何初始化C中的結構指針?
struct PipeShm
{
int init;
int flag;
sem_t *mutex;
char * ptr1;
char * ptr2;
int status1;
int status2;
int semaphoreFlag;
};
這工作正常:
static struct PipeShm myPipe = { .init = 0 , .flag = FALSE , .mutex = NULL ,
.ptr1 = NULL , .ptr2 = NULL , .status1 = -10 , .status2 = -10 ,
.semaphoreFlag = FALSE };
但是,當我宣佈static struct PipeShm * myPipe
,不工作,我假設我需要與運營商進行初始化->
,但如何?
static struct PipeShm * myPipe = {.init = 0 , .flag = FALSE , .mutex = NULL ,
.ptr1 = NULL , .ptr2 = NULL , .status1 = -10 , .status2 = -10 ,
.semaphoreFlag = FALSE };
是否可以聲明一個指向結構的指針並使用它的初始化?
您需要分配內存的指針,以便你可以做任何有意義的事情吧。指針本身只是指向一個隨機地址。您需要確保指針指向的地址足夠大以容納結構內容。 – 2012-07-29 14:19:43
我不明白...爲什麼你需要一個*指針*? – 2012-07-29 14:21:17
查看問題[here](http://stackoverflow.com/questions/2177391/allocating-memory-for-a-structure-in-c)爲了聲明一個指針,你需要分配內存,所以你的基本要求相同事情。 – rudolph9 2012-07-29 14:23:15