营销品牌网站建设,广告设计图片大全 创意,网站建设用语言,织梦网站上传保存文档先看题目#xff0c;写一个fun函数#xff0c;统计一个字符串中某个字符出现的次数#xff0c;以及这个字符第一次出现的位置。
看起来很简单#xff0c;似乎几行就可以搞定#xff0c;但是写出来之后#xff0c;才发现代码怎么这么长#xff01;程序里多处使用了指针写一个fun函数统计一个字符串中某个字符出现的次数以及这个字符第一次出现的位置。
看起来很简单似乎几行就可以搞定但是写出来之后才发现代码怎么这么长程序里多处使用了指针涵盖了下面三种用途
1动态申请内存返回指针 2用指针访问数组中的元素 3指针作为函数形参达到返回多个值的效果
题目面临的两个问题 1输入的字符串长度未知 2fun函数需要返回2个值
#include stdio.h
#include stdlib.hvoid fun(const char* s, char c, int* count, int* first_pos);int main()
{int size 10;char* s (char*)malloc(size); //①动态申请内存if (s NULL){printf(memory alloc error\n);return -1;}int length 0;char ch;while ((ch getchar()) ! \n){if (length size){size * 2;s (char*)realloc(s, size);if (s NULL){printf(memory alloc error\n);return -1;}}*(s length) ch; //②访问数组中的元素length;}*(s length) \0;printf(输入的字符串为%s\n, s);int count 0;int pos -1;char c a;fun(s, c, count, pos);printf(字符%c出现了%d次第一次出现的位置是%d\n, c, count, pos);free(s);return 0;
}void fun(const char* s, char c, int* count, int* first_pos) //③作为函数形参达到返回多个值的效果
{int cnt 0;int pos -1;int i 0;int found_flag 0;while (*s ! \0){if (*s c){if (found_flag 0){pos i;found_flag 1;}cnt;}s;i;}*count cnt;*first_pos pos;
} 以上程序多处用到了指针指针的所有操作和内存息息相关。 1动态申请内存返回指针 通过指针p获得对内存的控制权。 2用指针访问数组中的元素 数组本身是一个变量而变量呢它是内存中的一块区域所以通过指针访问数组中的元素实际上就是通过指针获得了变量所在内存的控制权。由于数组元素在内存中是连续分布的所以这个指针可以通过或者--操作访问数组里的元素这个指针它还描述了“位置信息”。 3指针作为函数形参达到返回多个值的效果 count是一个指针它指向了原来的实参通过指针可以直接修改原来的实参实际上通过指针获得了实参所在内存的控制权。
c语言的指针灵活但是容易出错。 程序追求三个目标“简洁、高效、安全”c程序的错误除了逻辑错误之外最多的就是内存错误“高效”是c语言的优点但是安全性……程序员一不小心就是崩溃。
下面用c改写程序看不到指针了程序更加“简洁、安全”。 1string是不定长字符串类型不用动态申请内存了。 2有了容器和迭代器可以不再使用c的指针了。 3使用引用代替指针。
#include iostream
using namespace std;void fun(const string s, char c, int count, int first_pos);int main()
{string s;cin s;cout 输入的字符串为 s endl;int count 0;int pos -1;char c a;fun(s, c, count, pos);cout 字符 c 出现了 count 次第一次出现的位置是 pos endl;return 0;
}void fun(const string s, char c, int count, int first_pos)
{int cnt 0;int pos -1;int i 0;int found_flag 0;for (auto elem : s) {if (elem c){if (found_flag 0){pos i;found_flag 1;}cnt;}i;}count cnt;first_pos pos;
}