找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 382|回复: 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 单片机教程网

快速回复 返回顶部 返回列表