C语言数组和指针的区别
一、定義
- 指針:表示C語言中某種數據類型的數據存儲的內存地址,例如,指向各種整型的指針或者指向某個結構體的指針;
- 數組:表示若干個相同C語言數據類型的元素在連續內存中儲存的一種形態。
二、區別
數組和指針本質上都代表一塊內存,數組名即代表這塊內存的地址,而指針本身不代表任何有意義的內容,只有給它賦值后,它才真正的表示一塊有意義的內存地址。
數組在編譯時就已經被確定下來,而指針直到運行時才能被真正的確定到底指向何方。
指針需要經過編譯、鏈接、運行時等重重考驗才能獲得屬于自己的內存。數組的這些內存一旦確定下來就不能輕易的改變了,這些內存會伴隨數組一生;而指針則有很多的選擇,在其一生他可以選擇不同的內存地址,比如一個字符指針可以指向單個字符同時也可代表多個字符等。
由于數組名直接代表其地址,而指針需要通過讀取其保存的地址才能讀取相應數據,所以數組為直接方式,而指針為間接訪問。
在編寫程序時,對于數組訪問應該寫成指針的形式,因為這樣可以提高效率。對于現代的編譯器而言,這個說法一般是錯誤的!現代編譯器對于數組的訪問都會自動優化為其對應的指針加偏移量的形式,所以也就沒有哪種形式效率更高的說法了。實際上,數組的訪問之所以改寫為指針加偏移量的方式,是因為其為系統底層最基本的工作方式。
在編寫程序時,對于數組訪問應該寫成指針的形式,因為這樣可以提高效率。對于現代的編譯器而言,這個說法一般是錯誤的!現代編譯器對于數組的訪問都會自動優化為其對應的指針加偏移量的形式,所以也就沒有哪種形式效率更高的說法了。實際上,數組的訪問之所以改寫為指針加偏移量的方式,是因為其為系統底層最基本的工作方式。
Problems became evident when I tried to extend the type notation, especially to add structured (record) types. Structures, it seemed, should map in an intuitive way onto memory in the machine, but in a structure containing an array, there was no good place to stash the pointer containing the base of the array, nor any convenient way to arrange that it be initialized. For example, the directory entries of early Unix systems might be described in C as
當我嘗試擴展類型符號,尤其是添加結構(記錄)類型時,問題變得很明顯。結構似乎應該以直觀的方式映射到機器的內存中,但是在包含數組的結構中,沒有好地方來存放包含數組基數的指針,也沒有任何方便的方法來安排它初始化。例如,早期 Unix 系統的目錄條目可能在 C 中描述為
我希望該結構不僅可以表征抽象對象,還可以描述可能從目錄中讀取的位集合。編譯器可以在哪里取命名語義要求的指針?即使結構被認為更抽象,并且指針的空間可以以某種方式隱藏,我如何處理在分配復雜對象時正確初始化這些指針的技術問題,也許是一個指定結構包含任意深度結構的數組的結構?
I wanted the structure not merely to characterize an abstract object but also to describe a collection of bits that might be read from a directory. Where could the compiler hide the pointer to name that the semantics demanded? Even if structures were thought of more abstractly, and the space for pointers could be hidden somehow, how could I handle the technical problem of properly initializing these pointers when allocating a complicated object, perhaps one that specified structures containing arrays containing structures to arbitrary depth?
The solution constituted the crucial jump in the evolutionary chain between typeless BCPL and typed C. It eliminated the materialization of the pointer in storage, and instead caused the creation of the pointer when the array name is mentioned in an expression. The rule, which survives in today’s C, is that values of array type are converted, when they appear in expressions, into pointers to the first of the objects making up the array.
該解決方案構成了無類型 BCPL(Basic Combined Programming Language) 和有類型 C 之間進化鏈中的關鍵跳躍。它消除了指針在存儲中的物化,而是在表達式中提到數組名時導致創建指針。在今天的 C 語言中仍然存在的規則是,當數組類型的值出現在表達式中時,它們會被轉換為指向組成數組的第一個對象的指針。
This invention enabled most existing B code to continue to work, despite the underlying shift in the language’s semantics. The few programs that assigned new values to an array name to adjust its origin—possible in B and BCPL, meaningless in C—were easily repaired. More important, the new language retained a coherent and workable (if unusual) explanation of the semantics of arrays, while opening the way to a more comprehensive type structure.
這項發明使大多數現有的 B 代碼能夠繼續工作,盡管語言的語義發生了潛在的變化。為數組名稱分配新值以調整其來源的少數程序(在 B 和 BCPL 中可能,在 C 中無意義)很容易修復。更重要的是,新語言保留了對數組語義的連貫且可行(如果不尋常)的解釋,同時為更全面的類型結構開辟了道路。
總結
以上是生活随笔為你收集整理的C语言数组和指针的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云VOD 视频点播(一)、nuxt视
- 下一篇: 杂项-数学软件:MATLAB