C语言基础 -35 指针_指针与一维数组
程序员文章站
2022-07-07 11:37:52
...
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
for(i = 0 ; i < sizeof(a)/sizeof(a[0]); i++)
printf("%d ",a[i]);
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
1 2 3
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
for(i = 0 ; i < sizeof(a)/sizeof(a[0]); i++)
printf("%p -> %d\n ",&a[i],a[i]);
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
0x7ffe76e43e6c -> 1
0x7ffe76e43e70 -> 2
0x7ffe76e43e74 -> 3
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
for(i = 0 ; i < sizeof(a)/sizeof(a[0]); i++)
printf("%p -> %d\n ",a+i,a[i]); // a+i 与&a[i]等效,表示a[i] 的地址
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
0x7fffed26c29c -> 1
0x7fffed26c2a0 -> 2
0x7fffed26c2a4 -> 3
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
int *p = a; //*p:指针取值 a是常量
for(i = 0 ; i < sizeof(a)/sizeof(a[0]); i++)
printf("%p -> %d\n ",p+i,a[i]);
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
0x7ffe3089f9ec -> 1
0x7ffe3089f9f0 -> 2
0x7ffe3089f9f4 -> 3
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
int *p = a;
for(i = 0 ; i < sizeof(a)/sizeof(a[0]); i++)
printf("%p -> %d\n",p+i,*(p+i)); // p+i 表示地址,指针为变量; *(p+i)为指针取值
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
0x7ffca5f2191c -> 1
0x7ffca5f21920 -> 2
0x7ffca5f21924 -> 3
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
int *p = a;
for(i = 0 ; i < sizeof(a)/sizeof(*a); i++) //a是常量,代表数值第一个地址;
//*a = *(a+0), 地址取值,代表a[0]
printf("%p -> %d\n",p+i,*(p+i));
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
0x7ffd7fe76c5c -> 1
0x7ffd7fe76c60 -> 2
0x7ffd7fe76c64 -> 3
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
int *p = a;
a++; //a是数组常量,不可用出现在等号左边,不可被赋值,否则后面编译会不通过
for(i = 0 ; i < sizeof(a)/sizeof(*a); i++)
printf("%p -> %d\n",p+i,*(p+i));
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
arr.c: In function ‘main’:
arr.c:10:3: error: lvalue required as increment operand
a++;
^~
<builtin>: recipe for target 'arr' failed
make: *** [arr] Error 1
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
int *p = a;
p++; //地址自增1
for(i = 0 ; i < sizeof(a)/sizeof(*a); i++)
printf("%p -> %d\n",p+i,*(p+i));
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
0x7ffe3fc586d0 -> 2 //输出从第2位元素开始
0x7ffe3fc586d4 -> 3
0x7ffe3fc586d8 -> -1548574976 //最后一位元素由于为赋值,故返回随机数
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3] = {1,2,3};
int i;
int *p = a;
p++;
printf("%p,%p\n",a,a+1);
printf("%p,%p\n",p,p+1);
for(i = 0 ; i < sizeof(a)/sizeof(*a); i++)
printf("%p -> %d\n",a+i,*(a+i));
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ ./arr
0x7ffd80569bcc,0x7ffd80569bd0
0x7ffd80569bd0,0x7ffd80569bd4
0x7ffd80569bcc -> 1
0x7ffd80569bd0 -> 2
0x7ffd80569bd4 -> 3
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3];
int i;
int *p = a;
p++;
printf("%p,%p\n",a,a+1);
printf("%p,%p\n",p,p+1);
for(i = 0 ; i < sizeof(a)/sizeof(*a); i++)
printf("%p -> %d\n",a+i,*(a+i));
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
0x7ffd16ea71ac,0x7ffd16ea71b0
0x7ffd16ea71b0,0x7ffd16ea71b4
0x7ffd16ea71ac -> 22092
0x7ffd16ea71b0 -> 384463520 //由于未赋初值,此处随机输出
0x7ffd16ea71b4 -> 32765
[email protected]:~/C_coding/CH01$ cat arr.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3];
int i;
int *p = a;
for(i = 0 ; i < sizeof(a)/sizeof(*a); i++)
scanf("%d",&a[i]);
for(i = 0 ; i < sizeof(a)/sizeof(*a); i++)
printf("%p -> %d\n",a+i,*(a+i));
printf("\n");
exit(0);
}
[email protected]:~/C_coding/CH01$ make arr
cc arr.c -o arr
[email protected]:~/C_coding/CH01$ ./arr
1
2
3
0x7fff74ff6a5c -> 1
0x7fff74ff6a60 -> 2
0x7fff74ff6a64 -> 3
上一篇: C语言strcmp函数
下一篇: C语言之一维数组与指针