當前位置:編程學習大全網 - 熱門推薦 - 指針與數組的關系是什麽啊

指針與數組的關系是什麽啊

1、指針:系統為某壹個變量開辟單元格,指針便指向此單元格的變量值。

2、數組:系統為某壹組數開辟壹組單元格,數組首地址便是妳定義的數組變量名。

數組和指針的唯壹區別是,不能改變數組名稱指向的地址。

對於數組來說,數組的首地址,也可以用指針來表示操作,如:

int a[10];

int *p,n;

p = a;

對第壹個元素取值,可以用幾種方法:

n =a[0];

n = *p;

n = p[0];

n = *(p+0) ;

但是以下語句則是非法的:

readings = totals; // 非法!不能改變 readings totals = dptr; // 非法!不能改變 totals

數組名稱是指針常量。不能讓它們指向除了它們所代表的數組之外的任何東西。

擴展資料

下面的程序定義了壹個 double 數組和壹個 double 指針,該指針分配了數組的起始地址。隨後,不僅指針符號可以與數組名稱壹起使用,而且下標符號也可以與指針壹起使用。

int main()

{

const int NUM_COINS = 5;

double coins[NUM_COINS] = {0.05, 0.1, 0.25, 0.5, 1.0};

double *doublePtr; // Pointer to a double

// Assign the address of the coins array to doublePtr

doublePtr = coins;

// Display the contents of the coins array

// Use subscripts with the pointer!

cout << setprecision (2);

cout << "Here are the values in the coins array:\n";

for (int count = 0; count < NUM_COINS; count++)

cout << doublePtr [count] << " ";

// Display the contents of the coins array again, but this time use pointer notation with the array name!

cout << "\nAnd here they are again:\n";

for (int count = 0; count < NUM_COINS; count++)

cout << *(coins + count) << " ";

cout << endl;

return 0;

}

程序輸出結果:

Here are the values in the coins array: 0.05 0.1 0.25 0.5 1 And here they are again: 0.05 0.1 0.25 0.5 1

當壹個數組的地址分配給壹個指針時,就不需要地址運算符了。由於數組的名稱已經是壹個地址,所以使用 & 運算符是不正確的。但是,可以使用地址運算符來獲取數組中單個元素的地址。

  • 上一篇:金瓶梅2高清在線觀看
  • 下一篇:水晶馬賽克怎麽貼正確貼墻磚,小家更溫馨!
  • copyright 2024編程學習大全網