指向数组的指针
#include
int main() {
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p = balance; // balance和p指向同一块地址
printf("使用指针的数组值\n");
for (int i = 0; i < 5; i++) {
printf("*(p + %d) : %.2f\n", i, *(p + i));
}
printf("使用 balance 作为地址的数组值\n");
for (int i = 0; i < 5; i++) {
printf("*(balance + %d) : %.2f\n", i, *(balance + i));
}
return 0;
}
使用指针的数组值
*(p + 0) : 1000.00
*(p + 1) : 2.00
*(p + 2) : 3.40
*(p + 3) : 17.00
*(p + 4) : 50.00
使用 balance 作为地址的数组值
*(balance + 0) : 1000.00
*(balance + 1) : 2.00
*(balance + 2) : 3.40
*(balance + 3) : 17.00
*(balance + 4) : 50.00
|