求字符串的长度(指针作参考)

来源:互联网 发布:centos 安装desktop 编辑:程序博客网 时间:2024/05/01 14:45
/** 程序的版权和版本声明部分* Copyright (c)2013, 烟台大学计算机学院学生* All rightsreserved.* 文件名称: fibnacci.cpp* 作    者:初瑞* 完成日期:2013  年 12 月  17 日* 版本号: v1.0* 输入描述:无* 问题描述:求字符串的长度(指针作参考)* 程序输出:* 程序输出:* 问题分析:*/#include <iostream>using namespace std;int astrlen(char *str);int main(){    char *str="hello world.";/*"hello world."是具体的了,所以第三行不能写成int astrlen(char *str[]);                               第十三行计算的是"hello world."的长度,如果在第六行写成char *str[]="hello world."                               就是计算str[]内的数组,这根本无法计算,所以是错误的                            */    astrlen(str);    cout<<"字符串的长度为"<<endl;    cout<<astrlen(str)<<endl;}int astrlen(char *str){  int i;   for(i=0;str[i]!='\0';i++);   return i;}

运算结果:

心得体会:我对指针的各种概念还是分辨不清,不知道上面写的注释是否正确。

0 0
原创粉丝点击