大佬教程收集整理的这篇文章主要介绍了数组和指针的关系(区别)详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
// This program shows an array name being dereferenced with the * operator. #include <iostream> using namespace std; int main() { short numbers[] = {10,20,30,40,50}; cout << "The first element of the array is "; cout << *numbers << endl; return 0; }程序输出结果:
The first element of the array is 10
numbers 在上面程序中的作用类似于指向数组起始地址的指针,所以当 numbers 被解引用时,第一个元素被检索出来。那么,如何使用间接运算符来检索数组的全部内容呢?请记住,数组元素是一起存储在内存中的,如图 1 所示。//This program processes an array using pointer notation. #include <iostream> using namespace std; int main() { const int SIZE = 5; // Size of the array int numbers[SIZE]; // Array of Integers // Get values to store in the array // Use pointer notation instead of subscripts cout << "Enter " << SIZE << " numbers: "; for (int count = 0; count < SIZE; count++) cin >> *(numbers + count); // Display the values in the array // Use pointer notation instead of subscripts cout << "Here are the numbers you entered:\n"; for (int count = 0; count < SIZE; count++) cout << * (numbers + count) << " "; cout << endl; return 0; }程序输出结果:
Enter 5 numbers: 5 10 15 20 25
Here are the numbers you entered:
5 10 15 20 25
// This program uses subscript notation with a pointer // variable and pointer notation with an array name. #include <iostream> #include <iomanip> using namespace std; 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
// This program uses the address of each element in the array. #include <iostream> #include <iomanip> using namespace std; int main() { const int nUM_COINS = 5; double coins[NUM_COINS] = {0.05,1.0}; double *doublePtr; // Pointer to a double //Use the pointer to display the values in the array cout << setprecision (2); cout << "Here are the values in the coins array:\n"; for (int count = 0; count < NUM_COINS; count++) { doublePtr = &coins[count]; cout << *doublePtr << " "; } cout << endl; return 0; }程序输出结果:
Here are the values in the coins array:
0.05 0.1 0.25 0.5 1
double readings[20],@R_58_10586@ls[20];
double *dptr;
dptr = readings; // 使 dptr 指向 readings
dptr = @R_58_10586@ls; // 使 dptr 指向 @R_58_10586@ls
readings = @R_58_10586@ls; // 非法!不能改变 readings
@R_58_10586@ls = dptr; // 非法!不能改变 @R_58_10586@ls
以上是大佬教程为你收集整理的数组和指针的关系(区别)详解全部内容,希望文章能够帮你解决数组和指针的关系(区别)详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。