久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標(biāo)題: 關(guān)于C語(yǔ)言的const結(jié)構(gòu)指標(biāo)用法問題 [打印本頁(yè)]

作者: keyway    時(shí)間: 2024-3-18 23:50
標(biāo)題: 關(guān)于C語(yǔ)言的const結(jié)構(gòu)指標(biāo)用法問題
如何讓 Disp()共用 const 結(jié)構(gòu)與RAM 結(jié)構(gòu)

#include <iostream>
#include <cstring>

typedef struct {
                uint8_t reg[2];
               }ST_t;

const ST_t  AAVal = {44,55};  // 副程式必須加 "const"
ST_t  BBVal = {11,22};
//void Disp(const ST_t *Ptr)  // 是否可以不用加"const"
void Disp(ST_t *Ptr)     
     {
      printf("AAVal[0] = %d   AAVal[1] = %d \n",Ptr->reg[0],Ptr->reg[1]);
     }
   
int main(void)
   {
    Disp(&AAVal);  
//  Disp(&BBVal);  // error: invalid conversion from ‘const ST_t*’ to ‘ST_t*’ [-fpermissive]
    printf("AAVal[0] = %d AAVal[1] = %d \n",AAVal.reg[0],AAVal.reg[1]);
    return (0);
   }



作者: Hephaestus    時(shí)間: 2024-3-19 00:33
我用mingw_w64 13.0編譯你的代碼,只有warning沒有error,你用的是什么編譯器?
作者: devcang    時(shí)間: 2024-3-19 11:54

編譯參數(shù)中不將warning當(dāng)錯(cuò)誤,通常是警告的
作者: keyway    時(shí)間: 2024-3-19 21:03
Hephaestus 發(fā)表于 2024-3-19 00:33
我用mingw_w64 13.0編譯你的代碼,只有warning沒有error,你用的是什么編譯器?

為什么 void Disp(ST_t *Ptr) 無(wú)法取 const 資料??
該如何改比較正確?

#include <iostream>
#include <cstring>

typedef struct {
                uint8_t reg[2];
               }ST_t;

const ST_t  AAVal = {44,55};  // "Flash 里"
ST_t  BBVal = {11,22};        // "RAM里"
/**********************************************/
//void Disp(const ST_t *Ptr)  // 是否可以不用加"const"
void Disp(ST_t *Ptr)     
     {
      printf("Val[0] = %d   Val[1] = %d \n",Ptr->reg[0],Ptr->reg[1]);
     }
/**********************************************/   
int main(void)
   {
    Disp(&AAVal);  
    Disp(&BBVal); // error: invalid conversion from ‘const ST_t*’ to ‘ST_t*’ [-fpermissive]

    return (0);
   }

我使用 jyshare站的/compile/12/   與 onlinegdb 編輯
作者: Hephaestus    時(shí)間: 2024-3-19 22:17
keyway 發(fā)表于 2024-3-19 21:03
為什么 void Disp(ST_t *Ptr) 無(wú)法取 const 資料??
該如何改比較正確?

-fpermissive
Downgrade some required diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive allows some nonconforming code to compile. Some C++ diagnostics are controlled only by this flag, but it also downgrades some C and C++ diagnostics that have their own flag:

-Wdeclaration-missing-parameter-type (C and Objective-C only)
-Wimplicit-function-declaration (C and Objective-C only)
-Wimplicit-int (C and Objective-C only)
-Wincompatible-pointer-types (C and Objective-C only)
-Wint-conversion (C and Objective-C only)
-Wnarrowing (C++ and Objective-C++ only)
-Wreturn-mismatch (C and Objective-C only)

The -fpermissive option is the default for historic C language modes (-std=c89, -std=gnu89, -std=c90, -std=gnu90).


作者: keyway    時(shí)間: 2024-3-21 00:03
Hephaestus 發(fā)表于 2024-3-19 22:17
-fpermissive
Downgrade some required diagnostics about nonconformant code from errors to warnings ...

出現(xiàn)error的原因是C++設(shè)計(jì)的比C更加安全,它不能自動(dòng)的將void *轉(zhuǎn)換為其它指針類型
解決方法:CFLAGS = -fpermissive
但 Mplab xc8與Keil 如何設(shè)定此參數(shù)?
作者: Hephaestus    時(shí)間: 2024-3-21 00:09
keyway 發(fā)表于 2024-3-21 00:03
出現(xiàn)error的原因是C++設(shè)計(jì)的比C更加安全,它不能自動(dòng)的將void *轉(zhuǎn)換為其它指針類型
解決方法:CFLAGS =  ...

單片機(jī)就不要蹭這個(gè)問題了,PC上面const變量好歹是在RAM里面只是不讓你改,單片機(jī)的const根本就沒法動(dòng)。
作者: 1600277881    時(shí)間: 2024-3-21 11:30
調(diào)用時(shí)轉(zhuǎn)換一下就行了
Disp((ST_t*)&BBVal);
作者: keyway    時(shí)間: 2024-3-21 22:33
1600277881 發(fā)表于 2024-3-21 11:30
調(diào)用時(shí)轉(zhuǎn)換一下就行了
Disp((ST_t*)&BBVal);

謝謝~~原來(lái)指標(biāo)也可以強(qiáng)制轉(zhuǎn)換為其它類型





歡迎光臨 (http://m.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 国产免费无遮挡 | 亚洲精品久久久久久久久 | 久久久久国产精品夜夜夜夜夜 | 一区二区三区久久久 | 91看片在线观看 | 91小视频在线观看 | 国产3p视频 | 国产一级黄色 | 精品视频在线观看免费 | 久久久久久久网站 | 日韩欧美在线观看 | 午夜精品视频在线 | 亚洲视频一区 | 国产资源在线播放 | 99热播| 中文字幕在线免费播放 | 91精品在线免费观看 | 欧美日韩精品 | av黄色在线 | 成人免费黄色大片 | 成年视频在线观看 | 国产麻豆一区二区 | 黄色免费在线视频 | 又黄又爽的网站 | 欧美激情视频在线 | 亚洲免费久久 | 国产精品一区二区在线免费观看 | 谁有毛片网站 | 久久久精品影院 | 国产suv精品一区 | 亚洲午夜天堂 | 涩色av | 久久av片| 欧美精品一区二区三区四区 | 97久久久久 | 欧美一级黄色录像 | 欧美精品99久久久 | 亚洲怡春院 | txvlog.com| 小日子的在线观看免费第8集 | 欧美日韩视频在线 |