我有以下檔案結構。我知道指標需要分配記憶體。檔案 A 由達芬奇生成。主要是我寫的。
// File A
typedef struct {
int v1;
}s1;
struct s2{
s1 *p1;
};
extern const struct s2 * const p3; // Expr 1
#define untilp1() (*(&p3->p1)) // Expr 2
// File main
#include <stdio.h>
#include "FileA.h"
int main()
{
int v2 = 10;
untilp1()->v1 = v2; // This line results in failure
return 0;
}
Q1 - 我不理解運算式 1。具體來說,我不理解 expr 1 中使用這么多 const 關鍵字。我知道術語 extern 的含義。在這種情況下,我猜常量指標p3是未定義、未宣告的,但編譯器知道它指向常量結構s2。這樣對嗎?如果你能說得更清楚,請詳細說明。
Q2 - 我不明白運算式 2。具體來說,我不明白(*(&p3->p2)). 我知道 的意思#define。請詳細解釋運算式。
Q3 - 在一般編碼中,我malloc在宣告使用指標之前分配記憶體,例如 using 。我不確定 DaVinci 生成這些檔案時是如何處理的。但是,我還沒有看到任何使用malloc等有誰知道,如果我能的價值分配我的同事v2來v1使用p3或untilp1()?
謝謝你。
uj5u.com熱心網友回復:
Q1:p3是一個const指標(意??思是指標本身不能改變)指向一個也不能改變的const struct s2意思struct s2。
Q2:*(&p3->p1)獲取struct p3的成員的地址,p1然后取消參考它。使用該宏定義,分配需要是:(untilp1())->v1 = v2;. 請參閱運算子優先級。我的建議是將括號放在宏中。
Q3:“一般編碼” - 我認為這需要一個單獨的問題。它可能是基于意見的。
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/346517.html
上一篇:無法將POST變數插入資料庫
下一篇:指標(GoLang)
