栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C/C++ const修饰指针

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C/C++ const修饰指针

const类型

  • const修饰指针
  • const修饰常量
  • const修饰指针和常量
#include 

int main(){
    int a = 1;
    int b = 2;

    //1. const修饰指针 ----> 常量指针
    const int *p = &a;
    // *p = 3; 报错,不能修改指针的值,只能修改它的指向
    p = &b; 
    printf("p指向的值为 %dn", *p);

    //2. const修饰指针 ----> 指针常量
    int *const q = &a;
    // q = &b; 报错,只能修改指针的值,不能去修改它的指向
    *q = b;
    printf("q指向的值为 %dn", *q);

    //3. const修饰指针和常量 
    const int  *const r = 3;
    

}

应用场景

#include
#include

struct Student{
    char name[100];
    int age;
    int score;
};



void printStu(const struct Student *s){
    printf("姓名: %s  年龄: %d  分数: %dn",s.name, s.age, s.score);
}

int main(){
    struct Student s = {"Garrix", 27, 100};
    // printStu(s); 一般用指针传递,这样系统开销会小很多,因为每次值传递,系统就会复制一份结构体
    printStu(&s);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000676.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号