首页 > 其他分享 >关于C语言的常量

关于C语言的常量

时间:2024-04-29 19:11:54浏览次数:19  
标签:const 常量 int value C语言 关于 constant pointer change

#include <stdio.h>

int main() {

    // (1)指向整型常量的指针。【const pointer】  地址:可变,值:不可变
    const int *p1; // 等价于 int const *p2;
    printf("[[constant pointer]] before change address: %p\n", p1);
    int a = 1;
    p1 = &a;
    printf("[[constant pointer]] after change address: %p\n\n", p1);

    // (2)指向整型常量的指针。【pointer to const】  地址:不可变,值:可变
    int b = 120;
    int *const p3 = &b;
    printf("[[pointer to const]] before change value: %d\n", *p3);
    *p3 = 123;
    printf("[[pointer to const]] after change value: %d\n\n", *p3);

    // (3)指向整型常量的常量指针。【const pointer to a const】  地址:不可变,值:不可变
    const int *const p4; // 等价于 int const *const p5;
    printf("[[constant pointer to a constant]]: NEITHER CAN CHANGE [addr] NOR [value]");
    return 0;
}
[[constant pointer]] before change address: 0000000000000000
[[constant pointer]] after change address: 00000025175ff98c

[[pointer to const]] before change value: 120
[[pointer to const]] after change value: 123

[[constant pointer to a constant]]: NEITHER CAN CHANGE [addr] NOR [value]
Process finished with exit code 0

标签:const,常量,int,value,C语言,关于,constant,pointer,change
From: https://www.cnblogs.com/cloucodeforfun/p/18166510

相关文章

  • 关于设备像素比
    因为高dpi会把原本低dpi的图片或者canvas拉伸导致模糊,所以在定义的时候我们让画布和画布的内容(因为canvas和内容是两个东西)都放大,这样保证高清,个人理解,如有更好的想法可及时更改1.画布尺寸调整:首先,我们需要根据设备的DPI或像素比调整Canvas的物理尺寸。这意味着如果设备的DPI较高......
  • C语言输入输出
    #include<stdio.h>intmain(){//练习:计算圆的面积,其半径由用户指定floatradius;//圆的半径printf("enterradius:");scanf("%f",&radius);//理解为阻塞式函数constfloatPI=3.14;floatarea=PI*radius*radius;p......
  • 关于零长度数组的思考
    首先看一下以下的结构声明structPacket{intcmd;intlen;charbody[0];};可以看到body被声明为一个长度为0的字符数组。经过测试,sizeof(Packet)的值为8,也就是说body实际上并没有分配内存。这种数组被称作零长度数组(ArraysofLengthZero)或者柔性数组,其中cha......
  • p1140 C语言循环数
    #include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#defineN100intmain(){intn,x,y,i,j,p,q,t,s;chara[N]={'0'};while(scanf("%s",&a)!=EOF){intb[N];......
  • 实验3 C语言函数应用编程
    1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明......
  • 【C语言】---- return的作用
    return是C语言中的一个关键字,用于从函数中返回值。它有以下几个作用:1返回值return用于将函数的结果返回给调用者。在函数执行过程中,当遇到return语句时,函数将立即停止执行,并将其后的表达式的值作为函数的返回值返回给调用者。例如:```cintadd(inta,intb){return......
  • C语言常量
    多种方式定义常量 常量没有数据类型。#include<stdio.h>#defineZERO0#definePI3.1415intmain(){//1.字面常量3.14;//字面常量1000;//字面常量//2.#defineprintf("zero=%d\n",ZERO);//ZERO=1;//不可以重新赋值/......
  • 【C语言】---- 数组
    在计算机编程中,数组是一种非常重要的数据结构,它可以用来存储多个相同类型的数据。在本文中,我们将深入探讨一维数组和二维数组,它们的定义、特性以及在编程中的应用。一维数组一维数组是最简单的数组形式之一,它是一组按顺序排列的元素的集合,每个元素都有一个唯一的索引。在C语言中......
  • 实验3 C语言函数应用编程
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明......
  • C语言,实现数字谱到简谱的转换
    C语言,实现数字谱到简谱的转换前言:本文初编辑于2024年4月28日CSDN:https://blog.csdn.net/rvdgdsva博客园:https://www.cnblogs.com/hassle前言使用C语言实现了一个程序,能够将数字谱转变成简谱网站能够识别的格式,依靠简谱网站将简谱绘制出来简谱网站,不需要安装任何应用,支持免......