開發平台(Platform): (Ex: Win10, Linux, ...)
單晶片開發 PIC18
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
CX8 compiler
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
None
問題(Question):
關於這段code指標的操作覺得不太正常, 想請教版友幫我確認觀念
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
定義與宣告部分:
typdef struct{
int A;
char *data;
} MSG_OBJ
typedef struct{
int B;
char useData[8];
} MSG_OBJ_USE
char globalData[8] = {0};
void getDataFun(MSG_OBJ* b)
{
b->data = globalData;
}
MSG_OBJ_USE obj;
以上是定義與宣告, 接下來呼叫時傳入obj (特別處為obj之type是MSG_OBJ_USE)
getDataFun(&obj);
之後這段code執行
//Step.1
char* ptr = (char*)(*(uint16_t*)obj.useData); //平台指標為16 bits
//Step.2
for(int i=0;i<8;i++);
{
obj.useData[i] = ptr[i];
}
//問題:
Step1.的動作意思是useData array上面放的值是指標, 所以才可以取值(*)給ptr
然後用step2把copy資料.
我的疑問是b->data = globalData應該是把&globalData[0] 指定給b->data而已.
謝謝回答!!
補充說明(Supplement):