Files
CWithClasses/C/BasicSyntax/Pointer/pointer_test_array.cpp
2025-12-31 00:39:23 +08:00

48 lines
873 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
//理解p与&a含义一致赋值只能通过p=&a。*p表示解把p地址解析为内存中的值
//const
//#define x 0
int x = 0;
/*void change1(int *arr){
*(arr+1) = -1;
} */
void change(int arr[5]){
arr[1] = -1;
}//效果一致
int main()
{
//只能通过地址给指针赋值(给指针赋值的是地址)
//用指针的时候需要初始化,否则会指向随机地址
int * p;
//p = &x; //初始化或者使用malloc指向随即内存地址
//int a[5];//尝试不开数组 ,用指针
/*
for(int i=0;i<5;i++){
scanf("%d",&a[i]);
}*/
for(int i = 0;i < 5 ; i++) {
p = &x;
p++;
}
/*for(p = a; p < (a + 5) ; p++){
scanf("%d",p);
}*/
p = &x;
for(int i = 0 ; i < 5 ; i++){
scanf("%d",p++);
}
//p = a;
//* (p+2) = 0;
//change(a);
p = &x;
for(int i=4 ; i >= 0 ; i--){
printf("%d ",*(p+i));
}
/*printf("\n");
for(int i=0;i<5;i++){
printf("%d ",*(p+i));
}*/
// printf("%d",* p);
return 0;
}