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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1284|回復: 8
打印 上一主題 下一主題
收起左側

關于C語言的const結構指標用法問題

[復制鏈接]
跳轉到指定樓層
樓主
ID:903667 發表于 2024-3-18 23:50 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
如何讓 Disp()共用 const 結構與RAM 結構

#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);
   }


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:883242 發表于 2024-3-19 00:33 | 只看該作者
我用mingw_w64 13.0編譯你的代碼,只有warning沒有error,你用的是什么編譯器?
回復

使用道具 舉報

板凳
ID:277550 發表于 2024-3-19 11:54 | 只看該作者

編譯參數中不將warning當錯誤,通常是警告的
回復

使用道具 舉報

地板
ID:903667 發表于 2024-3-19 21:03 | 只看該作者
Hephaestus 發表于 2024-3-19 00:33
我用mingw_w64 13.0編譯你的代碼,只有warning沒有error,你用的是什么編譯器?

為什么 void Disp(ST_t *Ptr) 無法取 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 編輯
回復

使用道具 舉報

5#
ID:883242 發表于 2024-3-19 22:17 | 只看該作者
keyway 發表于 2024-3-19 21:03
為什么 void Disp(ST_t *Ptr) 無法取 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).

回復

使用道具 舉報

6#
ID:903667 發表于 2024-3-21 00:03 | 只看該作者
Hephaestus 發表于 2024-3-19 22:17
-fpermissive
Downgrade some required diagnostics about nonconformant code from errors to warnings ...

出現error的原因是C++設計的比C更加安全,它不能自動的將void *轉換為其它指針類型
解決方法:CFLAGS = -fpermissive
但 Mplab xc8與Keil 如何設定此參數?
回復

使用道具 舉報

7#
ID:883242 發表于 2024-3-21 00:09 | 只看該作者
keyway 發表于 2024-3-21 00:03
出現error的原因是C++設計的比C更加安全,它不能自動的將void *轉換為其它指針類型
解決方法:CFLAGS =  ...

單片機就不要蹭這個問題了,PC上面const變量好歹是在RAM里面只是不讓你改,單片機的const根本就沒法動。
回復

使用道具 舉報

8#
ID:1088185 發表于 2024-3-21 11:30 | 只看該作者
調用時轉換一下就行了
Disp((ST_t*)&BBVal);
回復

使用道具 舉報

9#
ID:903667 發表于 2024-3-21 22:33 | 只看該作者
1600277881 發表于 2024-3-21 11:30
調用時轉換一下就行了
Disp((ST_t*)&BBVal);

謝謝~~原來指標也可以強制轉換為其它類型
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 欧美精品日韩少妇 | 免费av在线网站 | 日韩视频在线观看 | 久热精品在线 | 日韩精品国产精品 | 99久久综合| 天天精品 | 在线观看日韩精品 | 国产一区二区福利 | 亚洲三级在线观看 | 国产网友自拍 | 日本一级大毛片a一 | 欧美天天干 | 国产精品一区二区免费 | 日本一区二区不卡视频 | 国产www视频| 精品九九九 | 黄色大片av | 久久精品免费观看 | 国产精品久久久久久99 | 成人黄色一级片 | 国产三级在线 | 中文字幕的 | 中文在线字幕免费观 | 中文字幕av在线 | 夜夜夜夜操 | 美女无遮挡网站 | 久久免费观看视频 | 日韩欧美国产高清91 | 色资源在线 | 黄色网av | 在线不欧美 | 秘密爱大尺度做爰呻吟 | 国产天天操 | 91亚洲国产 | 欧美综合一区二区 | 成人免费看片 | 三级黄色网 | 一二三四区在线观看 | 亚欧av在线 | 日韩hd |