0
struct A
{
struct one
{
int c;
} details[MAX];
};
struct B
{
struct two
{
int a;
A::one b
} details[MAX];
};
有使用象下面來驗證結構的細節陣列結構A中的驗證函數:如何將指針傳遞給結構數組?
bool ValidateOne(struct one * ptr)
{
struct one * tmp[MAX];
for (int i = 0; i < MAX; ++i)
tmp[i] = ptr+i;
Validate(tmp[0]);
}
則存在驗證需要在細節陣列結構B的完成: 我期望做:
bool ValidateTwo(struct two * ptr)
{
struct one * tmp[MAX];
for (int i = 0; i < MAX; ++i)
tmp[i] = &((ptr+i)->b);
Validate(tmp[0]);
//validate other stuff
};
Validate(struct one * ptrs[])
{
int count;
for (int i = 0; i < MAX; ++i)
count += ptrs[i]->a;
}
上面的代碼能工作嗎?
你的代碼沒有多大意義。 'B :: two'聲明擁有'struct one'類型的成員 - 但是沒有這樣的類;有一個名爲'A :: one'的類。 'ValidateOne'函數試圖訪問名爲'one :: a'的成員 - 但'struct one'沒有這樣的成員。 –
編輯爲使用A :: one – Medicine
「ValidateOne」需要一個「one」數組 - 一個內存塊,其中多個「one」實例按順序並排放置。不過,'tmp'是一個指向'one'的指針數組 - 指向並排的指針的內存塊,但它們指向的實際對象不是。 'struct two'沒有'ValidateOne'期望的形式的內存塊:沒有大量的指針技巧會產生一個。 –