void char* speak(){ cout<<"zzz"<<endl;}
时间: 2023-08-20 19:32:08 浏览: 39
这是一个函数声明,但是语法错误,应该将 `void` 和 `char*` 的位置交换,同时在函数体内缺少返回值。
正确的函数声明应该是:
```
char* speak(void);
```
而函数定义应该是这样的:
```
char* speak(void) {
cout << "zzz" << endl;
return nullptr;
}
```
这个函数的作用是在控制台输出 "zzz",然后返回一个空指针。注意到函数的返回类型是 `char*`,但是在函数体内并没有返回任何字符串,因此返回一个空指针是合法的。
相关问题
请运行一下下面的代码int main() { int a = 10; int * p; p = &a; //指针指向数据a的地址 cout << *p << endl; //* 解引用 cout << sizeof(p) << endl; cout << sizeof(char *) << endl; cout << sizeof(float *) << endl; cout << sizeof(double *) << endl; system("pause"); return 0; }
好的,这段代码的作用是定义一个整型变量 a 并赋值为 10,然后定义一个整型指针 p,将其指向 a 的地址,输出指针所指向的数据(即 a 的值),以及指针 p 的大小,以及不同类型指针的大小。
代码如下:
```c++
#include <iostream>
using namespace std;
int main() {
int a = 10;
int *p;
p = &a;
cout << *p << endl;
cout << sizeof(p) << endl;
cout << sizeof(char *) << endl;
cout << sizeof(float *) << endl;
cout << sizeof(double *) << endl;
system("pause");
return 0;
}
```
运行结果为:
```
10
8
8
8
8
请按任意键继续. . .
```
其中,指针 p 的大小为 8 字节,这是因为在 64 位操作系统中,指针的大小为 8 字节;而 char、float、double 类型的指针大小也均为 8 字节。
#include <iostream> #include <cstring> using namespace std; #define SIZE 100 char buf[SIZE]; char* p = buf; char* alloc(int n) { char* begin; if (p + n <= buf + SIZE) { begin = p; p = p + n; return(begin); } else return(NULL); } int main() { char* p1, * p2; int i; p1 = alloc(10); strcpy(p1, "123456789"); p2 = alloc(5); strcpy(p2, "abcd"); cout << "buf=" << (void*)buf << endl; cout << "p1=" << (void*)p1 << endl; cout << "p2=" << (void*)p2 << endl; cout << p1 << endl; cout << p2 << endl; for (i = 0; i < 15; i++) cout << buf[i]; return 0; }修改它使得用vs能运行
这段代码在Visual Studio中需要进行以下修改才能运行:
1. 将头文件的包含语句从一行分成两行:
```
#include <iostream>
#include <cstring>
```
2. 去掉"using namespace std;",改为使用std::前缀。
3. 将strcpy函数改为strcpy_s函数。strcpy_s函数是一个安全的函数,它会检查目标数组的大小,防止缓冲区溢出。
4. 将指向buf的指针p的初始化改为以下形式:
```
char* p = &buf[0];
```
最终修改后的代码如下:
```
#include <iostream>
#include <cstring>
#define SIZE 100
char buf[SIZE];
char* p = &buf[0];
char* alloc(int n) {
char* begin;
if (p + n <= buf + SIZE) {
begin = p;
p = p + n;
return(begin);
}
else
return(NULL);
}
int main() {
char* p1, * p2;
int i;
p1 = alloc(10);
strcpy_s(p1, 11, "123456789");
p2 = alloc(5);
strcpy_s(p2, 6, "abcd");
std::cout << "buf=" << (void*)buf << std::endl;
std::cout << "p1=" << (void*)p1 << std::endl;
std::cout << "p2=" << (void*)p2 << std::endl;
std::cout << p1 << std::endl;
std::cout << p2 << std::endl;
for (i = 0; i < 15; i++)
std::cout << buf[i];
return 0;
}
```
经过以上修改,该代码能够在Visual Studio中成功编译和运行。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)