const int *a和int*const a 的區別詳解

補充知識

  • “const int i”與“int const i”之間的區別
  • 對變量來說,const 關鍵字可以限定一個變量的值不允許改變,從而保護被修飾的東西,防止意外修改,在一定程度上可以提高程序的安全性和可靠性。

代碼

const int *

int i1 = 10;
int i2 = 20;
const int *p = &i1;
/* 輸出結果是10 */
printf("%d\n", *p);
p = &i2;
/* 輸出結果是20 */
printf("%d\n", *p);
i2 = 30;
/* 輸出結果是30 */
printf("%d\n", *p);
  • 為什么 p 的值是可以被修改的,它可以重新指向另一個地址呢
  • 這里的 const 關鍵字修飾的是整個“*p”,而不是 p。所以這里的“*p”是不能被賦值的,也就是說我們不能通過“*p”來修改 i2 的值。
  • 其次,p 前并沒有用 const 關鍵字進行修飾,所以 p 是指針變量,能被賦值重新指向另一內存地址。也就是說下面的代碼是合法的:p = &i2; i2 = 30;
  • “int*const p”這種聲明形式。很顯然,這里的 const 是寫在 p 前和 * 號后的,而不是寫在“*p”前的,所以它是用來修飾限定 p 的

int *const

int i1 = 10;
int i2 = 20;
int *const p = &i1;
/* 輸出結果是10 */
printf("%d\n", *p);
/* p=&i2; p不能再這樣重新賦值了,即不能再指向另一個新地址*/
/* 可以通過*p修改i1的值*/
i1 = 30;
/* 輸出結果是30 */
printf("%d\n", *p);
  • p 因為有了 const 的修飾,所以只是一個指針常量。因此,這里的 p 值是不能重新賦值修改的,它只能永遠指向初始化時的內存地址。即下面的代碼是不合法的:
  • p = &i2; //p不能再這樣重新賦值了,即不能再指向另一個新地址
  • 但是,也正因為這里的整個“*p”的前面沒有 const 修飾。也就是說,“*p”是變量而不是常量,所以我們可以通過“*p”來修改它所指內存 i1 的值。因此,下面的語句是合法的:
  • 由此可見,如果關鍵字 const 直接寫在“*p”前,則程序不能修改“*p”,但可以修改 p;如果關鍵字 const 直接寫在 p 前,則程序不能修改的是 p,但可以通過“*p”來修改它所指內存的值。理解這兩點很重要,否則很難掌握“const int*p”與“int*const p”兩者之間的根本區別。

總結

const int i=10;
int *p;
/* 強制類型轉換*/
p= (int *) &i;
printf("*p=%d\n",*p)
/*這種賦值是合法的*/
*p=20;
printf("i=%d\n",i);
printf("*P=%d\n",*p);
  • 因為 const int 類型的 i 的地址是不能賦值給指向 int 類型地址的指針 p 的(否則 p 豈不是能修改i的值)。因此下面的語句是不合法的:p = &i
  • 可以通過強制類型轉換進行賦值,因此下面的這種賦值方法是合法的? p= (int *) &i;? *p=20;
  • 盡管可以通過強制類型轉換進行賦值,也不能通過“*p=20”來修改 i 的值。因此,“printf("i=%d\n”,i)”輸出的結果是 10,并不是 20。
  • 示例運行結果為:*p=10? i=10? *p=20

第一個是指針,第二個是指針所指物

p是指指針,*p是指指針所指物

const *p,限制的是(*p),*p不可以動,p可以動

* const? p 限制的是(p),p不可以動,*p可以動

參考鏈接

  • C語言const int *a和int*const a 的區別詳解

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446605.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446605.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446605.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

codeforces 133A-C語言解題報告

133A題目網址 題目解析 1.輸入字符串,如果里面包含H,Q,9,就輸出YES,否則輸出NO 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h>int main() {char c[100]{0};int flag0;int i;scanf("%s",c);for(i0;i<strlen(c);i){if(c[i]H…

c++面向對象高級編程 學習十 function-like classes

本節是設計一個class&#xff0c;使它的行為像一個函數。 如果一個東西能接受小括號()操作符&#xff0c;那么這個東西就稱之為函數&#xff0c;或像函數的東西。 下圖為三個函數對()的重載&#xff0c;這三個類均為像函數的類&#xff0c;它們可接受()操作符&#xff0c; 標…

數據結構 棧

代碼 #include <stdio.h> #include <stdlib.h>/************************************************************************/ /*棧應用示例--數制轉換要求&#xff1a;輸入任意的正整數N(十進制)&#xff0c;分別輸出該整數的二進制、八進制、十六進制的結果算法…

英語口語 Week14 Monday

英語文章 Thailand, a country in Southeast Asia with an area of about 514,000 square kilometers, has been increasingly prosperous in its tourism industry in the past few decades. Its capital is Bangkok and its major languages are Thai, Chinese and English.…

c++面向對象高級編程 學習十一 類模板、函數模板、成員模板

namespace經驗談&#xff1a; 團隊中函數或類的名字可能會沖突&#xff0c;因此使用namespace進行區分。 類模板&#xff1a; template<typename T> 函數模板&#xff1a; template<class T>&#xff0c;此處class可改成typename 函數模板在使用的時候&#xff0…

操作系統面試 總結

以下文章來源于程序員cxuan &#xff0c;作者cxuan 原文鏈接什么是操作系統 操作系統是管理硬件和軟件的一種應用程序。操作系統是運行在計算機上最重要的一種軟件&#xff0c;它管理計算機的資源和進程以及所有的硬件和軟件。它為計算機硬件和軟件提供了一種中間層&#xff…

英語口語week 14 Thursday

英語文章 A couple decided to go out to celebrate their wedding anniversary, so they called a babysitter. When the babysitter arrived, the two children had already been asleep. The babysitter soon got bored and went to the kitchen where she blended some wh…

c++面向對象高級編程 學習十二 模板

模板特化&#xff1a; 模板是一種泛化的形式&#xff0c;特化是將參數類型進行指定&#xff0c;寫出特化的版本&#xff0c;當在調用下圖cout<<hash()(1000);的時候&#xff0c;由于特化中有struct hash{ }的版本&#xff0c;因此會直接調用特化部分。 模板偏特化&…

英語口語 week14 Friday

英語文章 Shopping is taking place every second. However, the prices of the same goods may differ from store to store. A name-brand dress may cost several hundred pounds at a boutique, but only half the price in a discount store or a big chain store. Moreo…

c++面向對象高級編程 學習十三 數量不定的模板參數,auto,for

文章目錄數量不定的模板參數autoranged-base for數量不定的模板參數 void print() {} //... 表示參數的數量不定 template<typename T, typename...Types> void print(const T&firstArg, const Types&...args) {cout << firstArg << endl;cout<&…

數據結構 樹

定義 樹是節點的優先集合度&#xff1a;孩子的數量&#xff0c;度為0 就是終端節點&#xff0c;不為零就是根節點有序樹&#xff1a;有順序&#xff0c;不可以替換無序樹&#xff1a;無順序&#xff0c;可以替換深度 和 樹的深度相反&#xff0c;第一層深度為1 樹的深度為 3 二…

英語口語 Week15 TuesDay

英語文章 One day, when Bella was doing sports in the school yard, the squirrel fled out of her sleeve. Threading its way through a considerable number of people, the squirrel disappeared in the distance After a sequence of movements, it hopped onto the ar…

c++面向對象高級編程 學習十四 引用

文章目錄referencereference的常見用途reference 變量有三種形式&#xff1a;值&#xff0c;指針&#xff0c;引用 int x0; //值 int* p&x;//指向整型的指針&#xff0c;地址&#xff0c;指針在之后的程序中可以指向其他變量 int& rx;//引用&#xff0c;此處表示 r代…

google瀏覽器 隱藏功能開啟

網址 chrome://flags/ 1&#xff0c;多線程下載 2&#xff0c;暗黑模式3&#xff0c;標簽縮略圖4&#xff0c;PWA 漸進式web應用 網頁即應用5&#xff0c;閱讀模式&#xff0c;排除廣告&#xff0c;點擊閱讀模式去除干擾chrome://net-internals6&#xff0c;解決有問題的代理IP…

英語口語Week 15 Wednesday

英語文章 Accomplishing the task assigned by the teacher; Julia rushed out. Squatting at the gate and playing with the squirrel, Bella waved at the sight of Julia and yelled out here" . Julia ran quickly towards them, pointed at the squirrel and asked…

c++面向對象高級編程 學習十五 組合繼承關系下的構造和析構

文章目錄繼承關系組合關系繼承和組合繼承關系 構造由內而外&#xff0c;析構由外而內&#xff0c;內即是父類 組合關系 A擁有B&#xff0c; 構造由內而外&#xff0c;析構由外而內&#xff0c;內即是B 繼承和組合 構造和析構順序如圖&#xff1a;

英語口語Week16 Wednesday

英語文章 Recently my friend received a gift from her boyfriend - a very expensive bracelet. But the substance of her response left us in astonishment - she didn’t attend to the exquisiteness(of the gift and wanted to return it to him In terms of salary, …

C++ 查漏補缺

特性關系 C語言面向過程C面向過程 面向對象(封裝 繼承 多態)C具備C語言的全部特性的基礎上&#xff0c;并且支持更多新的特性 內存泄露 申請內存&#xff0c;沒有釋放申請 malloc new釋放 free deleteProcessExplorer查看內存是否釋放 代碼移植 將生成的exe運行在別的平臺&…

c++面向對象高級編程 學習十六 vptr和vtbl

當一個類中有一個或多個虛函數時&#xff0c;內存中會多一個虛指針&#xff08;vptr&#xff0c;virtual pointer&#xff09;&#xff0c;指向一個虛表&#xff08;vtbl&#xff0c;virtual table&#xff09; 父類有虛函數&#xff0c;則子類一定有虛函數 在下圖示意圖中&a…

英語口語Week16 Thursday

英語文章 It is an impossibility that everything runs smoothly in everyday life. Where there is trouble, there could be anxiety.Anxiety is a common phenomenon; you are not the only one carrying it. But, it could be somewhat poisonous if you don’t let it o…