C指针原理(17)-C指针基础
2018-12-02 21:40
239 查看
指针本身也是一种变量,支持常用的运算。比如加、减
#include <stdio.h> int main(void){ int i; char x[20]="0123456789ABCDEFGHIJ"; for (i=0;i<20;i++){ printf("x[%d]:%c\n",i,x[i]); } char *p_x; for (p_x=&x[0];p_x<&x[20];p_x++){ printf("%c",*p_x); } printf ("\n"); for (p_x=x;p_x<(x+20);p_x++){ printf("%u:%c\n",p_x,*p_x); } return 1; }
在后面增加的内容p_x直接指向了x,然后通过x++来移动指针,终止for循环的条件是p_x<(x+20),x+20指向了数组的结尾处后面的内存位置,这个内存位置永远不会被访问,所以这样引用是安全的。
最后这段代码将每个字符的内存地址以及字符本身输出.
myhaspl@myhaspl:~ % make clean
rm mytest
myhaspl@myhaspl:~ % make
cc test3.c -o mytest
myhaspl@myhaspl:~ % ./mytest
...........
...........
4294957776:0
42949 5b4 57777:1
4294957778:2
4294957779:3
4294957780:4
4294957781:5
4294957782:6
4294957783:7
4294957784:8
4294957785:9
4294957786:A
4294957787:B
4294957788:C
4294957789:D
4294957790:E
4294957791:F
4294957792:G
4294957793:H
4294957794:I
4294957795:J
myhaspl@myhaspl:~ %
相关文章推荐
- C指针原理(17)-C指针基础
- C指针原理 (25)-C指针基础
- C指针原理(33)-C指针基础
- C指针原理(96)-C基础综合应用
- C指针原理(14)-C指针基础
- Python基础(17)_面向对象程序设计(抽象类、继承原理、封装、多态,绑定方法)
- C指针原理(18)-C指针基础
- C指针原理(19)-C指针基础
- 蓝鸥iOS从零基础到精通就业-C语言入门 17指针二
- C指针原理(16)-C指针基础
- C指针原理 (26)-C指针基础
- C指针原理(15)-C指针基础
- C语言基础-指针和数组的关系17
- C++基础7【难】 多态:实现原理 vptr指针 证明vptr存在 类的步长 纯虚函数:抽象类 案例 【面试题】
- C指针原理(21)-C指针基础
- OC基础-静态类型,动态类型,编译检查,运行检查,父子类指针和对象互指17
- Effective C++ 学记之17 以独立语句将newed对象置入智能指针
- socket编程基础-原理和代码
- 黄聪:Cookie工作基础原理、数据如何移动、Web站点怎么使用Cookies
- C指针原理(8)