C语言struct student *creat()表示什么意思
时间: 2023-12-18 09:05:49 浏览: 163
`struct student *creat()` 是一个函数声明,它表示一个返回类型为 `struct student *` 的函数名为 `creat`。在C语言中,函数名后面的括号中可以包含函数的参数列表,但是在这个声明中,括号中没有参数,因此这是一个不带参数的函数声明。
这个函数声明表示 `creat` 函数会返回一个指向 `struct student` 类型的指针。通常情况下,`creat` 函数的作用是创建一个 `struct student` 类型的对象,并返回指向该对象的指针。在函数体内部,可以通过动态内存分配函数(如 `malloc`)来申请一块内存来存储 `struct student` 对象,并初始化该对象的成员变量。
例如,下面的示例代码定义了一个 `struct student` 结构体,然后定义了一个名为 `creat` 的函数,该函数会动态分配内存来创建一个 `struct student` 对象,并返回指向该对象的指针:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
int id;
char name[20];
};
struct student *creat() {
struct student *p = (struct student *) malloc(sizeof(struct student));
p->id = 1001;
strcpy(p->name, "Tom");
return p;
}
int main() {
struct student *s = creat();
printf("ID:%d\n", s->id);
printf("Name:%s\n", s->name);
free(s); // 释放动态分配的内存
return 0;
}
```
程序输出:
```
ID:1001
Name:Tom
```
这里的 `creat` 函数返回了一个指向动态分配的 `struct student` 对象的指针,然后在 `main` 函数中调用该函数,并使用返回的指针访问 `struct student` 对象的成员变量。最后,记得释放动态分配的内存。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)