C++指针数组,二级指针和函数指针的练习
- 1.编一程序,将字符串“Hello,C++!”赋给一个字符数组,
然后从第一个字母开始间隔地输出该串(请用指针完成)。 - 代码如下
#include<iostream>
#include<string>
using namespace std;
int main() {
const char str[] = "Hello,C++!";
const char *p = str;
while ((*p) != '\0')
cout << *p++ << " ";
cout << endl;
return 0;
}
测试截图
2.编写一个函数,用于去掉字符串尾部的空格符。
函数原型为:char *mytrim(char *string);
其中参数string为字符串,返回值为指向string的指针。- 代码如下:
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
char *mytrim(char *string) {
char *p = string;
int c1 = 0, c2 = 0;
while ((*p++) != '\0')
c1++;
cout << "The original length of the string is:" << c1 << endl;
for (int i = c1 - 1; string[i] == ' '; i--) //Reverse Search for spaces
c2++;
*(string + (c1- c2)) = '\0';
cout << "The string is now long:" << c2 << endl;
return string;
}
int main() {
char str[] = "Hello,C++! ";
cout << "The original string is:";
printf("%s\n",str);
cout << mytrim(str) << endl;
return 0;
}
- 测试截图
- 3.编写一个函数,用于去掉字符串前面的空格。
函数原型为:char *myltrim(char *string);
其中参数string为字符串,返回值为指向string的指针。 - 代码如下
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
char *mytrim(char *string) {
char *p = string;
int c1 = 0, c2 = 0;
while ((*p++) != '\0')
c1++;
cout << "The original length of the string is:" << c1 << endl;
for (int i = 0; string[i] == ' '; i++) //Find spaces from Start
c2++;
for (int i = 0; i < c2; i++)
string[i] = string[i + c2];
*(string + (c1 - c2)) = '\0';
cout << "The string is now long:" << c2 << endl;
return string;
}
int main() {
char str[] = " Hello,C++!";
cout << "The original string is:";
printf("%s\n", str);
cout << mytrim(str) << endl;
return 0;
}
- 测试截图
C++指针数组,二级指针和函数指针的练习的更多相关文章
- c语言指针数组和结构体的指针
指向数组的指针,先初始化一个数组,使用传统方式遍历 void main() { ] = { ,,,, }; ; i < ; i++) { printf("%d,%x\n", ...
- c语言.函数指针数组
函数指针: 一个指向函数的指针.一般用函数名表示. 函数指针数组:元素为函数指针的数组.转移表.c语言中函数不可以定义为数组,只能通过定义函数指针来操作. #include<stdio.h> ...
- 转:函数指针数组的妙用(I)
转自:http://blog.sina.com.cn/s/blog_4c78b35f010008hi.html 笔者在开发某软件过程中遇到这样一个问题,前级模块传给我二进制数据,输入参数为 char* ...
- C语言函数指针变量和指针函数以及指针数组
C语言中,一个函数总是占用一段连续的内存区,而函数名就是该函数所占内存区的首地址.我们可以把函数的这个首地址(或称入口地址)赋予一个指针变量,使该指针变量指向该函数.然后通过指针变量就可以找到并调用这 ...
- C语言基础知识点整理(函数/变量/常量/指针/数组/结构体)
函数 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
- [C++]数组指针,数组引用,函数指针
数组指针是指一个指向数组的指针,例如有一个数组指针p指向一个数组a[],则 *p是取到这个数组,也就是说 *p=a,因此 **p =a[0], 它的定义为: ]; ]=&a; (*c)表示它是 ...
- [C++ Primer Plus] 第7章、函数(一)程序清单——递归,指针和const,指针数组和数组指针,函数和二维数组
程序清单7.6 #include<iostream> using namespace std; ; int sum_arr(int arr[], int n);//函数声明 void ma ...
- 指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数
1.指针数组 数组里面的每一个元素都是指针. 指针数组的案比例如以下: 易犯错误: 2.数组指针 归根结底还是指针,仅仅是取*的时候可以取出一整个数组出来. 数组指针:(一个指针指向了数组.一般 ...
- C++中的指针、数组指针与指针数组、函数指针与指针函数
C++中的指针.数组指针与指针数组.函数指针与指针函数 本文从刚開始学习的人的角度,深入浅出地具体解释什么是指针.怎样使用指针.怎样定义指针.怎样定义数组指针和函数指针.并给出相应的实例演示.接着,差 ...
- 【嵌入式开发】C语言 命令行参数 函数指针 gdb调试
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21551397 | http://www.hanshul ...
随机推荐
- 用js md5加密
/* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...
- js实现图片延时加载的原理
实现原理: 附:(http://www.cnblogs.com/fishtreeyu/archive/2011/03/12/1982067.html) 把所有需要延时加载的图片改成如下的格式: < ...
- Oracle数据库错误消息
Oracle数据库错误消息 导出错误消息 l EXP-00000导出终止失败 原因:导出时产生Oracle错误. 操作:检查相应的Oracle错误消息. l EXP-00001数据域被截断 - 列长度 ...
- 《ArcGIS Runtime SDK for Android开发笔记》——问题集:Error:Error: File path too long on Windows, keep below 240 characters
1.前言 在使用Android Studio开发环境时,经常会爆出以下错误,虽然具体细节内容各有不同,但是说明的都是同一个问题,在windows中使用过长的路径,超过240字符. Error:Erro ...
- keras 多输出问题
转自:https://github.com/Xls1994/DeepLearningCode/blob/master/Keras/HedgeScope/multiOutputLSTM.py
- Rational ClearCase 系列
Base + UCM 系列参考:http://blog.csdn.net/hhg208/article/details/5749050
- UML视频
https://www.bilibili.com/video/av34973179/?p=1 北京 圣思园 UML视频
- 关于git stash的应用总结
Step1 新增 git stash save -a "message" // 对于在项目里加入了代码新文件的开发来说,-a选项才会将新加入的代码文件同时放入暂存区 类似于 git ...
- Oracle分区表分批迁移
遇到个分区表数据量超大的case,磁盘空间不够,所以考虑使用数据泵分批导数据,测试如下: source : oracle windows 32bit 10.2.0.1target : oracle ...
- March 1 2017 Week 9 Wednesday
If you are serious giving up something, giving up is not serious at all. 如果你慎重地决定要放弃,那么放弃本身就没什么大不了的. ...