bool myatoi(const char *s,int &num)
{
cout<<(&s)<<endl;
num=;
while (*s)
{
if ((*s)>=''||(*s)<='')
{
num=num*+((*s)-'');
}
else
return false;
s++;
//cout<<"The address of pstr is: "<<static_cast<void*>(const_cast<char*>(str))<<endl;
cout<<"prt:"<<(void*)s<<endl;
}
return true;
}
int main()
{
char *s="";
int num;
myatoi(s,num);
cout<<num<<endl;
char s1='';
char s2='';
char s3=s1-s2;
cout<<s3<<endl;
int i=s1-s2;
cout<<i<<endl;
return ; }

首先要分清楚:字符数组和字符串的关系!
字符串存放在数组中,因此,一个字符数组可以存放几个串,单字符串函数只认字符串结束标志'\0';
1. strlen(wer wer):字符串为"wer_wer"这种字符串常量,系统会在其后自动补上'\0';而求字符串长度的函数strlen()只要遇见'\0';就返回函数值!而且'\0'不算在其中!故返回值为7(空格也算一个字符!)
2. strlen(wer\0wer) 其中的字符串为"wer\0wer"而strlen函数遇到'\0'即结束,故返回值为:3
3. '\0'不是空格,也不是回车!通过ASCII码表,你可知道,'\0'是ASCII码值代表0(NULL);而空格的ASCII码为:  '\32' 32 回车的ASCII码值为'\13'  13

所以while (*s)当s++到最后*s=NULL,循环结束。

注意打印字符串指针不能使cout<<s;

因为<<操作符重载了吧,会默认输出字符串的值。必须显示转换为(void *)才可取得地址。

规范写法:cout<<"The address of pstr is: "<<static_cast<void*>(const_cast<char*>(str))<<endl; 类型转换安全
        cout<<"prt:"<<(void*)s<<endl;这样也可以。

注意cout<<&s;输出的是字符串指针的地址。

自己实现atoi的更多相关文章

  1. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. 编写atoi库函数

    看到很多面试书和博客都提到编写atoi函数,在很多面试中面试官都会要求应聘者当场写出atoi函数的实现代码,但基本很少人能写的完全正确,倒不是这道题有多么高深的算法,有多么复杂的数据结构,只因为这道题 ...

  3. 行程编码(atoi函数)

    #include<iostream> #include<string> #include<vector> using namespace std; void jie ...

  4. No.008:String to Integer (atoi)

    问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  5. c/c++面试题(8)memcopy/memmove/atoi/itoa

    1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...

  6. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

  7. [LeetCode] 8. String to Integer (atoi)

    Implement atoi to convert a string to an integer. public class Solution { public int myAtoi(String s ...

  8. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

  9. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  10. 【leetcode】atoi (hard) ★

    虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过  “      123” ...

随机推荐

  1. Cisco交换机配置VLAN

    Cisco IOS中有两种方式创建vlan,在全局模式下使用vlan vlanid命令,如switch(config)#vlan 10; 在vlan database 下创建vlan ,如 switc ...

  2. Servlet下载文件迅雷不支持问题真相之一

    问题描述 最近在做一个下载文件的Servlet,直接使用浏览器的下载功能,完美支持,结果测试人员使用迅雷下载,就不行了,下载也能成功完成,只是迅雷下载的文件大小是悲催的0KB 真相搜罗 网上有很多帖子 ...

  3. Oracle表明明存在SQL查询数据提示表不存在异常

    今天同事遇到一个很奇怪的问题,恢复了一个数据库,表明明存在,用PLSQL和sqlplus都试过了,SQL语句select * from 表名,查询数据,却提示表名不存在异常 然而,使用select * ...

  4. git默认忽略文件的大小写

  5. 软渲染 SoftRender

    弄了这几年OpenGL对管线还是算比较熟悉,写起来也比较顺,不过每个顶点都要经过一堆变换,着实感到效率的重要.(矩阵乘顶点没有SSE加速啊)装个B,半天的成果..(主要很多东西都写好了直接拿来) // ...

  6. 续:纠正:debian【4】可以安装,而且完美的安装 ! for《Oracle-10.2.0.1,打补丁10.2.0.5:在 debian 版本4【不含4】以上,及 ubuntu 7.04【不含7.04】以上都可以安装!》

    关键点: ip a ifconfig -a dhclient ifconfig -a poweroff ip a ifconfig -a apt-get update apt-get install ...

  7. SmbException: 0xC000007F

      The error code 0xC000007F means: NT_STATUS_DISK_FULL There is not enough space on the disk. https: ...

  8. sql2008修改管理员与普通用户密码

    方法一: sp_password Null,'123,'sa'把sa的密码设为“123” 执行成功后有“Command(s) completed successfully.” OK! 方法二: 第一步 ...

  9. Ubuntu18.10安装网易云音乐(图文并茂!)

    听音乐,怎么少得了网易云音乐,下面我们在Ubuntu18.10上来安装下: 首先进入网易云音乐的下载页:https://music.163.com/#/download,选择下载Ubuntu版本: 我 ...

  10. IT运维队伍的管理

    如何建设一支能够解决问题.创造价值.有活力的.不断进取的IT运维团队,并带领这支团队,充分发挥这个团队的优势力量,是运维业务有效开展的关键.运维 策略是直接体现运维业务的经济价值所在.好的运维措施.方 ...