C语言学习:指针

今天重温了C 语言指针相关的内容,简单的说指针也是变量,可以是 int,char,float等类型,指针存储的是地址,下面的这段代码能充分说明指针的特性,记录下:

Point.c 代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*Program: pointer.c*/
#include <stdio.h>

int main(void)
{
int number=10;
int *p1=NULL;

printf("nnumber's address: %p",&number);
printf("nnumber's values: %dn",number);

p1=&number; /*Store the address of number in pointer*/

printf("npointer's address: %p ",&p1); /*Output the address*/
printf("npointer's values: %p",p1); /*Output the value (an address )*/
printf("npointer's size: %d bytes",sizeof(p1)); /*Output the size*/

printf("nvalues pointed to: %dn",*p1);

return 0;
}

编译并执行

1
2
3
4
5
6
7
8
9
10
[pg92@redhatB point]$ gcc -o points points.c
[pg92@redhatB point]$ ./points

number's address: 0xbf865bac
number's values: 10

pointer's address: 0xbf865ba8
pointer's values: 0xbf865bac
pointer's size: 4 bytes
values pointed to: 10

备注:指针 p1 本身的地址为 0xbf865ba8,它存储的值为 0xbf865bac, 这个值正是整形变量 number 的地址。

指针用法图

C语言学习:指针
备注: 这个图能形象的说明指针。

参考

C语言入门经典(第四版)

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/237939.html

(0)
上一篇 2022年1月29日
下一篇 2022年1月29日

相关推荐

发表回复

登录后才能评论