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 ...
随机推荐
- FontSize sp 和 dp 的区别
dp不会随着“设置->显示->字体大小”的改变而改变,sp会. sp会随着configeration的配置来scale, dp不会. 所以,什么时候用sp, 什么时候用dp需要斟酌.
- 移动端下滑刷新插件(jQuery插件)
由于在工作不能独自开发,而且为了给他们方便,自己写过不少的插件,不过今天刚好空闲,发出刚好完成的,移动端的下滑到底刷新插件.我不是很喜欢写插件给别人用,因为用起来自然是简单的,没什么难度,所以一起分享 ...
- 抽象工厂模式&简单工厂模式
抽象工厂模式 优点: 如IFactory factory=new AccessFactory(),在一个应用中只需要初始化一次,这就使得改变应用的时候变得非常容易:其次它让具体的创建实例过程与客户端分 ...
- 弹出框layer插件
有时候我们在网页制作中需要引用各种弹出框,弹出框的展现形式多种多样.可以是弹出图片,视频,文字,也可以是弹出图片轮播等形式: 弹出框插件——layer使用方法(其实官方文档中已经介绍的很详细): 下载 ...
- CSS深入理解学习笔记之border
1.border-width border-width为何不支持百分比:语义和使用场景决定的,现实中各种边框本身的概念就不存在百分比的使用方法. border-width支持关键字:thin.medi ...
- Java使用imageio、awt生成图片验证码
1.生成验证码工具类 public class CheckCodeTool { private Integer width = 80; private Integer height = 38; pub ...
- SharePoint 2013 - Client Side Rendering
1. Client side rendering 代码结构为: (function () { // Create object that have the context information ab ...
- Android基础Activity篇——创建一个活动(Activity)
1.创建活动 首先用AS创建一个add no activity项目名使用ActivityTest,包名为默认的com.example.activitytest 2.右击app.java.com.exa ...
- css 字体样式设置大全
css样式大全(整理版) 字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 { ...
- ACCESS模糊查询注意事项
ACCESS模糊查询出现的问题,开发中需要注意!在SQL Server中模糊查询通常是这样的Select * from articleTable where authorName like '%jac ...