请教结构初始化的问题
有下列几个结构,当CPathTracer初始化时,同时怎样也给CBranchStack初始化,急盼指教.
struct CBranch
{
short m_x;
short m_y;
unsigned char m_dir;
};
typedef struct BranchNode
{
CBranch* branch;
short m_nPathLen;
BranchNode* prev;
BranchNode* next;
}BranchNode_t;
struct CBranchStack
{
BranchNode_t* m_pHead ;
BranchNode_t* m_pTail ;
};
struct CPathTracer
{
unsigned char* tm_pImage;
short tm_nWidth;
short tm_nHeight;
short tm_nDirOfst[8];
short tm_nPathLen;
CBranchStack* tm_BranchStack;
};
[解决办法]
struct CPathTracer tPathTracer;
tPathTracer.tm_BranchStack = (CBranchStack *)malloc(sizeof(CBranchStack));
tPathTracer.tm_BranchStack-> m_pHead = NULL;
tPathTracer.tm_BranchStack-> m_pTail = NULL;
[解决办法]
1.
typedef struct Ex
{
int a;
char b;
int c;
}Ex;
Ex A = {3, 'a ', 6};
你可以这样给他初始化
2.
#include <stdio.h>
#include <stdlib.h>
struct s1
{
char *s;
int i;
struct s1 *sip;
};
struct s1 a[] = { { "abcd ", 1, a+1}, { "efgh ", 2, a+2}, { "ijkl ", 3, a} };
[解决办法]
struct CPathTracer tPathTracer;
tPathTracer.tm_BranchStack = (CBranchStack *)malloc(sizeof(CBranchStack));
tPathTracer.tm_BranchStack-> m_pHead = NULL;
tPathTracer.tm_BranchStack-> m_pTail = NULL;
其中的tm_pImage貌似也要初始化
另外使用时最好加以判断指针是否为空.