一个很好的c++指针面试题 想当年我只对了一个,现在回头看,还是错的一塌糊涂
2016-03-01 17:01
399 查看
//参数为指针 实际上也是值传递 实参与型参是两个不同的概念
void GetMemory1(char *p, int num)
{
p = (char*)malloc(num);
}
void Test1(void)
{
char *str = NULL;
GetMemory1(str, 100);
strcpy(str, "hello world");
printf("%s\n", str);
}
//型参为指针的指针 实际也就是指针的引用 对指针的操作可以认为是同一个东西
void GetMemory2(char **p, int num)
{
*p = (char*)malloc(num);
}
void Test2(void)
{
char * str = NULL;
GetMemory2(&str, 100);
strcpy(str, "hello world");
printf("%s\n", str);
free(str);
}
char* GetMemory3(void)
{
//栈内存
char p[] ="hello world";
return p;
}
void Test3(void)
{
char* str = NULL;
str = GetMemory3();
printf("%s\n", str);
}
char* GetMemory4(void)
{
//常量区
char *p = "hello world";
return p;
}
void Test4()
{
char* str = NULL;
str = GetMemory4();
printf("%s\n", str);
}
char* GetMemory5(void)
{
char *p = (char*)malloc(100);
strcpy(p,"hello world");
return p;
}
void Test5()
{
char* str = NULL;
str = GetMemory5();
printf("%s\n", str);
free(str);
}
void Test6( void )
{
char * str = (char*)malloc(100);
strcpy(str, "hello");
free(str);
if (str != NULL)
{
strcpy(str, "world" );
printf("%s\n",str);
}
}
int main()
{
Test1();
while(1);
return 0;
}
void GetMemory1(char *p, int num)
{
p = (char*)malloc(num);
}
void Test1(void)
{
char *str = NULL;
GetMemory1(str, 100);
strcpy(str, "hello world");
printf("%s\n", str);
}
//型参为指针的指针 实际也就是指针的引用 对指针的操作可以认为是同一个东西
void GetMemory2(char **p, int num)
{
*p = (char*)malloc(num);
}
void Test2(void)
{
char * str = NULL;
GetMemory2(&str, 100);
strcpy(str, "hello world");
printf("%s\n", str);
free(str);
}
char* GetMemory3(void)
{
//栈内存
char p[] ="hello world";
return p;
}
void Test3(void)
{
char* str = NULL;
str = GetMemory3();
printf("%s\n", str);
}
char* GetMemory4(void)
{
//常量区
char *p = "hello world";
return p;
}
void Test4()
{
char* str = NULL;
str = GetMemory4();
printf("%s\n", str);
}
char* GetMemory5(void)
{
char *p = (char*)malloc(100);
strcpy(p,"hello world");
return p;
}
void Test5()
{
char* str = NULL;
str = GetMemory5();
printf("%s\n", str);
free(str);
}
void Test6( void )
{
char * str = (char*)malloc(100);
strcpy(str, "hello");
free(str);
if (str != NULL)
{
strcpy(str, "world" );
printf("%s\n",str);
}
}
int main()
{
Test1();
while(1);
return 0;
}
相关文章推荐
- 编程基础-----c语言打印调用栈
- c语法集锦
- C++链接两个cpp 文件
- C++第15周(春)项目3 - OOP版电子词典(一)
- C++ Primer 学习笔记——动态内存与智能指针(1)
- C++ primer 第五版 中文版 练习 10.37
- 二维数组回形遍历(北大信科2015上机考试)
- c语言的结构体详解
- c/c++在windows下获取时间和计算时间差的几种方法总结
- #if defined和#if !defined(c语言的宏定义)
- 2014第五届蓝桥杯C++B组第七题
- C语言中你必须知道的100个问题
- C语言实现单链表节点的删除(带头结点)
- 使用C++封装互斥锁的实践
- C++文件读写详解(ofstream,ifstream,fstream)
- C++内存管理
- C语言中switch语句的思考
- c++防止客户端多开巧妙代码
- C++ 虚函数表解析
- opencv error assertion failed,imread读到的总是空数据,Microsoft C++ 异常: 内存位置 0x0043f888 处的 cv::Exception。