2-1.c数组指针的定义:

#include <stdio.h>
int main()
{
char str[]="China Beijing Fujian"; //定义数组str并进行初始化
char *pstr; //定义指针变量pstr
pstr=&str[6]; //初始化pstr,并使指针变量pstr指向数组str
printf("str:%s,str");
printf("pstr指向str[6]:%c\n,*pstr");
return 0;
}

  结果:

str:China Beijing Fujian
pstr指向str[6]:B

2-2.c数组指针的引用

#include <stdio.h>
int main(void)
{
char str[]="China Beijing Fujian"; //定义数组str并进行初始化
char *pstr; //定义指针变量pstr
printf("str:%s\n",str); //输出数组str,%s表示字符串
pstr=str; //初始化指针变量pstr,pstr指向数组str;pstr和str指向数组str首地址
printf("pstr->str:%s\n",pstr); //以%s的形式输出pstr指向的数组元素
printf("&str=%p\n",&str); //%p指针的值,输出数组名str的地址,即&str。连续数组空间的首地址,即str[0]的地址
printf("pstr=%p\n",pstr); //输出指针变量pstr的值。因为pstr指向数组str,pstr的值就是str的地址,也是数组元素,str[0]的地址
printf("&str[0]=%p\n",&str[0]);
return 0;
}

  结果:

str:China Beijing Fujian
pstr->str:China Beijing Fujian
&str=0x7fffd12e1c80
pstr=0x7fffd12e1c80

 2-3

#include <stdio.h>
int main(void)
{
char str[]="China Fujian";
char *pstr;
pstr=str;
while(*pstr)
{
printf("%c:%p\n",*pstr,pstr); //%c单个字符,第一次执行时,pstr指向str[0],输出str[0]以及&str[0]
pstr++; //让指针指向下一个数组元素
}
return 0;
}

  结果:

C:0x7fff13c1f1b0
h:0x7fff13c1f1b1
i:0x7fff13c1f1b2
n:0x7fff13c1f1b3
a:0x7fff13c1f1b4
:0x7fff13c1f1b5
F:0x7fff13c1f1b6
u:0x7fff13c1f1b7
j:0x7fff13c1f1b8
i:0x7fff13c1f1b9
a:0x7fff13c1f1ba
n:0x7fff13c1f1bb

2-4.c数组的下标表示法

#include <stdio.h>
#define WEEKNUM 7 //定义一个宏
int main(void)
{
int temp;
int week[WEEKNUM]={1,2,3,4,5,6,7};
printf("please input today is:");
scanf("%d",&temp);
if(temp<=WEEKNUM)
{
printf("tomorrow is :%d\n",week[temp]); //%d十进制有符号字符
}
else
{
printf("Error \n");
}
return 0;
}

  结果:

please input today is:4
tomorrow i

2-5

#include <stdio.h>
#define LEN 8
int main(void)
{
char i;
char str[LEN]="Fujian";
printf("str:%s\n",str);
for(i=0;i<LEN;i++)
{
printf("%c:%p\n",str[i],&str[i]);
}
return 0;
}

    结果:

str:Fujian
F:0x7fff3f5c5d20
u:0x7fff3f5c5d21
j:0x7fff3f5c5d22
i:0x7fff3f5c5d23
a:0x7fff3f5c5d24
n:0x7fff3f5c5d25
:0x7fff3f5c5d26
:0x7fff3f5c5d27

2-6.c数组的指针表示法

#include <stdio.h>
#define LEN 10
int main(void)
{
char str[LEN]={'A','B','C','D','E','F','G','H','I','J'};
char idx,*pstr;
pstr=str;
printf("please input (0-9)and ENTER:\n");
scanf("%d",&idx);
if(idx<LEN)
{
printf("The character is:%c\n",*(pstr+idx)); //patr指向数组str,即指向数组的第一个元素str[0],当运行pstr+idx运算后,,指针pstr指向str[idx],*(patr+idx)就是数组元素str[idx]
}
else
{
printf("The idx is overflow \n");
}
return 0;

     结果:

please input (0-9)and ENTER:
4
The character is:E

2-8.c数组的下标法和指针

#include <stdio.h>
#define LEN 15
int main(void)
{
char str[LEN]="Fujian.2018";
char idx,*pstr;
for(idx=0;idx<LEN;idx++)
{
printf("%c",str[idx]); //下标法
}
printf("\n");
pstr=str;
while(*pstr)
{
printf("%c",*(pstr+idx)); //指针法
pstr++;
}
printf("\n");
return 0;
}

  结果:

Fujian.2018

2-9.c指针数组的引用 

#include <stdio.h>
int main(void)
{
char *str[]={"Fujian","Huian"}; //定义数组str并初始化
printf("str[0]:%s\n",str[0]); //%s一次性输出一个字符串,若要一次性输出一个字符串需要知道字符串的首地址,str[0]即为首地址
printf("str[1]:%s\n",str[1]);
return 0;
}

  结果:

str[0]:Fujian
str[1]:Huian

2-10

#include<stdio.h>
int main()
{
char temp;
char *str[]={"IllgalDay","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
printf("please input number:\n");
scanf("%d",&temp);
if(temp<7)
{
switch(temp)
{
case 0:
printf("Ingall day\n");
break;
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
}
}
return 0;
}

  结果:

please input number:
1
Monday

2-11.c字符串指针的引用

#include <stdio.h>
int main(void)
{
char *str="Fujian 2018"; //相当于 char *str; str ="Fujian 2018";
printf("%s\n",str); //指针变量str指向字符串的“Fujian 2018”的首地址,这里仅仅是指向不是赋值
return 0;
}

  结果:

Fujian 2018

2-12数组方式实现

#include <stdio.h>
int main(void)
{
char str[]="Fujian 2018"; //定义数组,并进行初始化
printf("%s\n",str);
return 0;
}

  结果:

Fujian 2018

利用数组形式定义字符串就需要知道字符串大小,而指针形式则没有这个限制

2-13  

#include <stdio.h>
int main()
{
char *str="Fujian 2018";
printf("%s\n",str); //%s字符串
printf("%p\n",str); //%p指针,输出str,字符串首地址
while(*str)
{
printf("%c",*str); //输出字符
printf(" %p\n",str); //输出字符地址
str++;
}
return 0;
}

  结果:

Fujian 2018
0x4006c8
F 0x4006c8
u 0x4006c9
j 0x4006ca
i 0x4006cb
a 0x4006cc
n 0x4006cd
0x4006ce
2 0x4006cf
0 0x4006d0
1 0x4006d1
8 0x4006d2

 一步一个脚印...... 

C指针(1)——指针在数组中的应用(程序讲解)的更多相关文章

  1. Java例题_30 在已经排好序的数组中插入值

    1 /*30 [程序 30 插入数字] 2 题目:有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. 3 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插 ...

  2. c++中的指针之指针在数组

    使用一维指针数组输出一维数组中的数 int array[]={1,2,3,4,5,6};        int *p; p=array;        for(int i=0;i<6;i++){ ...

  3. 把vector中的string对象导入到字符指针数组中

    #include <iostream>#include <string>#include <vector>//#include <cctype>#inc ...

  4. C++中指针和引用、数组之间的区别

    指针指向一块内存,它的内容是所指内存的地址:而引用则是某块内存的别名,引用初始化后不能改变指向.使用时,引用更加安全,指针更加灵活. 初始化.引用必须初始化,且初始化之后不能呢改变:指针可以不必初始化 ...

  5. 论C语言中二级指针和二维数组之间的区别

    刚开始学习C语言的时候,觉得一个数组可以定义一个一级指针去访问,想当然的就觉得可以定义一个二级指针去访问二维数组.很显然这是错误的. 我们来看看C语言的数组在内存中的存储方式. 实际上C语言中的数组, ...

  6. C 语言:返回两个数组中第一个相同元素的指针(我用了loop 、goto loop标签)

    // //  main.c //  Pointer_search // //  Created by ma c on 15/8/2. //  要求:通过指针查找,实现比较两个有序数组中的元素,输出两个 ...

  7. C语言:返回两个数组中第一个元素的指针,并输出这个值

    // //  main.c //  Pointer_search // //  Created by ma c on 15/8/2. //  Copyright (c) 2015年. All righ ...

  8. C++语言中数组指针和指针数组彻底分析

    #################################                              ##       基本知识               ##        ...

  9. C++基础 (8) 第八天 数组指针 模板指针 C语言中的多态 模板函数

    1昨日回顾 2 多态的练习-圆的图形 3多态的练习-程序员薪资 4员工管理案例-抽象类和技术员工的实现 employee.h: employee.cpp: technician.h: technici ...

  10. C++中的指针、数组指针与指针数组、函数指针与指针函数

    C++中的指针.数组指针与指针数组.函数指针与指针函数 本文从刚開始学习的人的角度,深入浅出地具体解释什么是指针.怎样使用指针.怎样定义指针.怎样定义数组指针和函数指针.并给出相应的实例演示.接着,差 ...

随机推荐

  1. 合并Gridview单元格

    Introduction There are a lot of methods in the Internet solving the problem of how to merge GridView ...

  2. Vue之组件间传值

    标签: Vue Vue之父子组件传值 父向子传递通过props 子向父传递通过$emit 演示地址 代码示例如下: <!DOCTYPE html> <html lang=" ...

  3. flexbox预习

    创建一个flexbox: .flex-container{ display:flex; } flex-direction:  column;//将flex排成一列 flex-direction: co ...

  4. android api 之Scroller

    Scroller是封装了滚动,实现View和ViewGroup的背景画布的滚动. 它有两个构造方法: public Scroller (Context context) 传递一个上下文. public ...

  5. mysql5.1安装图解教程

    mysql5.1安装图解教程 曾经为了要搭建模拟平台,从内地网站学习到台湾网站,其中必不可少的一项就是MySQL,那时候正好普遍用5.1,所以也很习惯用MySQL5.1版本! 下面就以MySQL5.1 ...

  6. matplotlib安装指南

    matplotlib安装指南1.介绍NumPy地址 http://www.numpy.org/NumPy:一个Python一个扩展库,支持大量的维度数组和举证运算,对数组提供大量的函数库 SciPy地 ...

  7. SQLServer 2012 Ent 安装失败,另辟蹊径

    今天配到SqlServer2012 安装不了问题,总是NetFX3,然后一次挂了,然后VS2010 Shell也是个悲剧. 装了VS2010,可以了吧,总不能在不给力吧.但是有提示VS2010是打上s ...

  8. ubuntu 安装 deb 软件包

    参考链接地址 blog.csdn.net/kevinhg/article/details/5934462 sudo dpkg -i xxxx.deb 安装一个 Debian 软件包,如你手动下载的文件

  9. MySQL数据库实验三:连接查询

    实验三    连接查询 实验名称:连接查询(2课时) 一.实验目的 理解JOIN语句的操作和基本使用方法,掌握内连接.外连接.自身连接的概念和使用. 二.实验环境 是MS SQL SERVER 200 ...

  10. Cmake 01

    1. sdsf(single direction single file) 1.1  The directory tree /* ./template | +--- build | +---main. ...