梳理了好长时间,总是分不清为什么一级指针能干的事儿为啥引入二级指针,据一个驱动工程师说还是挺常用的,忍者难受尝试使用了一下二级指针。

遇到一个问题是,如果想让一级指针指向移动,二级指针需要的格式是(*p)++,而不是*p++,这是和一级指针不同的地方,写了两个对比的小例子,如下:

1.数输入的单词个数,不使用二级指针

#include <stdio.h>

int wordcount(char *str){
int count;
int word = 0;
while(*str){
count = 0;
while((*str>='a' && *str<='z') || (*str>='A' && *str<='Z')){
count++;
str++;
}
if(*str == ' '){
if(count>2) word++;
str++;
continue;
}
else if(*str == '\0')
{
if(count>2) word++;
break;
}
else {
do{
str++;

}while(*str != ' ' && *str!= '\0');
}
}
return word;

}
int main(int argc, const char *argv[])
{
char str[100];
printf("please input a str:");
scanf("%[^\n]",str);

int ret = -1;

ret = wordcount(str);

printf("word in str:%d\n",ret);
return 0;
}

使用二级指针:

#include <stdio.h>

int count(char **p){

(*p)++;
if(**p == ' ' || **p == '\0')return 0;
while(**p !=' ' && **p != '\0'){
if(**p < 'A' || (**p > 'Z' && **p < 'a') || **p > 'z'){
do{
(*p)++;
}while(**p != ' ' && **p != '\0');
return 0;
}
else (*p)++;
}
return 1;

}

int wordcount(char *str){

int word = 0;

while(*str){

if(*str == ' '){
str++;
continue;
}
else if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z'))
{
word += count(&str);
}
else {
do{
str++;

}while(*str != ' ' && *str!= '\0');
}
}
return word;

}
int main(int argc, const char *argv[])
{
char str[100];
printf("please input a str:");
scanf("%[^\n]",str);

int ret = -1;

ret = wordcount(str);

printf("word in str:%d\n",ret);
return 0;
}

2.输入字符串,在字符串处插入hello,使用二级指针代码

#include <stdio.h>
void insert(char **p,char **q)
{

while(*p < *q){
*(*q+4) = **q;
(*q)--;
}
while(**q) (*q)++;
**p = 'h';
*(*p+1) = 'e';
*(*p+2) = 'l';
*(*p+3) = 'l';
*(*p+4) = 'o';
*p = *p+5;
}

void spacetohello(char *str){
char *t; //移动寻找space指针
char *tail; //数组尾指针指向\0
tail = str;
t = str;
while(*tail) tail++;

while(*t){
if(*t == ' ')
insert(&t,&tail);
else t++;

}
}

int main(int argc, const char *argv[])
{
char str[100];
printf("please input a str:");
scanf("%[^\n]",str);

printf("str before:%s\n",str);

spacetohello(str);

printf("str after:%s\n",str);
return 0;
}

不适用二级指针代码

#include <stdio.h>

void spacetohello(char *str){
char *t; //移动寻找space指针
char *tail; //数组尾指针指向\0
tail = str;
t = str;
while(*tail) tail++;

while(*t){
if(*t == ' '){
while(t < tail){
*(tail+4) = *tail;
tail--;
}
while(*tail) tail++;
*t = 'h';
*(t+1) = 'e';
*(t+2) = 'l';
*(t+3) = 'l';
*(t+4) = 'o';
t = t+5;
}
else t++;

}
}

int main(int argc, const char *argv[])
{
char str[100];
printf("please input a str:");
scanf("%[^\n]",str);

printf("str before:%s\n",str);

spacetohello(str);

printf("str after:%s\n",str);
return 0;
}

转载于:https://www.cnblogs.com/huiji12321/p/11151667.html

step1.day11 C语言基础练习之指针和二级指针相关推荐

  1. step1 . day8 C语言基础练习之指针和函数

    今天继续复习指针,还是很深奥的,两点注意事项: 1. int型数据可以强制类型转化赋值给指针变量,然后对该地址赋值(用在裸机上): 2.指针数组是数组,存放的是指针,数组指针是数组的指针,存放的是行指 ...

  2. C语言:结构体中一级指针和二级指针的创建与释放示例

    http://blog.csdn.net/Bixiwen_liu/article/details/53610952 这几天把C语言巩固了一下,作为一门最基本的编程语言,C语言还是相当基础和非常重要的, ...

  3. C语言什么时候必须用到二级指针?(需要调用函数为一维空指针确定值的情况下,需要传入二维指针,也就是那个一维指针的指针)

    结论:需要调用函数为一维空指针确定值的情况下,需要传入二维指针,也就是那个一维指针的指针 例子:为空指针p开辟内存空间 1.不用二级指针(无法实现) #include <stdio.h> ...

  4. 【C基础】指针/指针运算/二级指针/函数指针

    指针定义: 指针是一种数据类型,使用它可以用来定义指针变量,指针变量中存储的其实是整数,这种整数代表了内存的编号. 指针的使用: 1.函数之间相独立,但有些时候需要共享变量.传参是值传递全局变量容易命 ...

  5. 指针数组,数组指针,指针函数,函数指针,二级指针详解

    先看个简单的:char *p,这定义了一个指针,指针指向的数据类型是字符型,char  *(p)定义了一个指针P: char *p[4], 为指针数组,由于[]的优先级高于*,所以p先和[]结合,p[ ...

  6. 深入理解指针以及二级指针(指针的指针)

    前言:本文将讲解指针的定义.指针变量和普通变量的本质区别.一级指针和二级指针的关系以及如何通过二级指针修改一级指针所指向的内存.文末还附加了两个实例,帮助读者加深对二级指针的理解.本文试图通过图表的方 ...

  7. (C++)函数参数传递中的一级指针和二级指针

    主要内容: 1.一级指针和二级指针 2.函数指针传递的例子 3.什么时候需要传递二级指针? 4.二级指针在链表中的使用 1.一级指针和二级指针 一级指针:即我们一般说的指针,就是内存地址; 二级指针: ...

  8. 函数参数的传递问题(一级指针和二级指针)

    函数参数的传递问题(一级指针和二级指针) [转] 原以为自己对指针掌握了,却还是对这个问题不太明白.请教!   程序1:   void  myMalloc(char  *s)  //我想在函数中分配内 ...

  9. C和指针之二维字符串数组用指针数组、数组指针、二级指针打印

    1.问题 二位字符串数组用指针数组.数组指针.二级指针打印       2.测试代码 #include <stdio.h>int main() {char value[4][5] = {& ...

最新文章

  1. jasp报错_jetty启动访问jsp页面报错
  2. C#中Monitor和Lock以及区别
  3. VC中按钮控件的启用(enable)和禁用(disable)
  4. gridcontrol 控件的用法
  5. python 链表 【测试题】
  6. python open r w r+ w+ a的区别
  7. Intellij IDEA创建包(package)问题解决方案
  8. 微信号可以一年内第二次修改技巧吗?
  9. LeetCode:459.重复的子字符串 Python3 | 判断输入的字符串是不是可以由子串多次重复构成
  10. 背包问题(动态规划 C/C++)
  11. 如何利用PyTorch实现一个Encoder-Decoder结构进行英法互译
  12. 活体检测论文研读三:Learning Deep Models for Face Anti-Spoofing: Binary or Auxiliary Supervision
  13. Android手机直播(三)声音采集
  14. 基于PaddlePaddle实现的DeepSpeech2端到端中文语音识模型
  15. 20211202 做了接盘侠
  16. GC overhead limit exceede
  17. 如何设置tab缩进为4个字符
  18. 查询所有科目成绩都大于90分的学生姓名
  19. [zz from byhh]完整的阿里支付宝面试经过
  20. 【转】ultraedit 正则表达式

热门文章

  1. ajax get请求_JSP中的对讲机Ajax简述
  2. Python入门--数据类型
  3. 每天一道LeetCode-----计算最长的元素连续序列长度
  4. Apache(4)——配置文件里的各参数(2)
  5. 《Linux内核分析》课程总结
  6. 12.当效率至关重要时,请在map::operator[]与map::insert之间谨慎作出选择
  7. linux文件系统体系结构 和 虚拟文件系统(VFS)
  8. TCP连接中的异常情况
  9. Mysql查询数据库状态及信息
  10. C++ list::splice()用法