由搜狐的一道笔试题想到的
来源:互联网 发布:禁止电脑安装任何软件 编辑:程序博客网 时间:2024/05/22 01:53
题目如下:
#include<iostream> using namespace std; void swap_int(int a , int b) { int temp = a; a = b; b = temp; } void swap_str(char* a , char* b) { char* temp = a; a = b; b = temp; } int main(void) { int a = 10; int b = 5; char* str_a = "hello world"; char* str_b = "world hello"; swap_int(a , b); swap_str(str_a , str_b); printf("%d %d %s %s\n", a , b , str_a , str_b); return 0; }
题目就是 判断能否交换两个字符串和两个整数
同时考察 值传递和引用传递区别。
值传递编译器自动创建临时变量存放响应的变量,当函数返回变量的作用失效。
这道题目难点不在swap_int,在于swap_str
把这道题目换个方式写出来,我想可能更容易理解。
#include<iostream> using namespace std; void swap_int(int a , int b) { int temp = a; a = b; b = temp; } void swap_str(char* a , char* b) { char* temp = a; a = b; b = temp; } int main(void) { int a = 10; int b = 5; char *my_stra=NULL;char *my_strb = NULL; char *temp =NULL;char* str_a = "hello world"; char* str_b = "world hello";my_stra = str_a;my_strb = str_b ; swap_int(a , b); // swap_str(str_a , str_b); printf("%d %d %s %s\n", a , b , str_a , str_b); temp = my_stra;my_stra =my_strb;my_strb =temp;printf(" %s %s\n", str_a , str_b); return 0; }
实际上和上面的类似,这里my_stra和my_strb都是指针变量,指针变量的内存地址 分别是str_a和str_b;
此时我们交换my_stra和my_strb实际上只是对他们两个的地址做变换。
而str_a和str_b是不会有变化的。
- 由搜狐的一道笔试题想到的
- 由中兴一道笔试题想到的
- 由朗讯的一道笔试题想到的!
- 由朗讯的一道笔试题想到的
- 由朗讯的一道笔试题想到的!
- 255 搜狐的一道笔试题
- 由一道试题想到的
- struct 字对齐:-_- 由朗讯的一道笔试题想到的!
- 由一道笔试题想到的:原码反码和补码
- 由两道笔试题想到的
- 由一道面试题想到的
- 由一道数据结构题目想到的
- 由一道面试题目想到的
- 由一道高考题而想到的
- 从Google的一道笔试题想到的
- 想到一道考察C功底的笔试题
- 搜狐齐全的笔试题
- 搜狐齐全的笔试题
- mysql 如何修改数据库名称?
- RTP时间戳
- 堆栈
- x-windows pentest
- 一个简单的游戏服务器框架
- 由搜狐的一道笔试题想到的
- java.net.ConnectException: Connection refused: connect 完美解决(SQLserver2000)【转】
- 程序员的十大谎言
- 去除快捷方式图标小箭头
- Database Creation on 11.2 Grid Infracture with Role Separation ( ORA-15025, KFSG-00312, ORA-15081 )
- QWS
- set_error_handler 三种用法
- 使用NetBeans + Xdebug调试PHP程序[总结]
- ASP.NET MVC学习之 Html.ActionLink