fscanf(fp, "%d\n", &conf.number);//输入记录条数 int(*p)[3] = (int(*)[3])malloc(sizeof(int) * conf.number * 3);的意思
时间: 2023-08-28 15:31:49 浏览: 34
这段代码的作用是从文件中读取记录条数,并动态分配一块内存来存储记录信息。它首先使用fscanf从文件中读取一个整数,该整数表示记录的条数,并将其存储在conf.number中。接下来,它使用malloc函数动态分配一块内存,该内存大小为conf.number * 3 * sizeof(int) 字节,其中3表示每条记录中有3个整数,* sizeof(int) 表示每个整数所占的字节数。最后,它将返回的指针强制转换为一个二维整型数组指针p,以便后续将记录信息存储在该内存块中。
相关问题
#include<stdio.h> #include<stdlib.h> #define MAX 20 struct ENode{ int ivex[MAX], jvex[MAX]; //边依附的两个顶点// int weight[MAX]; //边的权值// }; struct ELGraph { int vexnum, edgenum; //顶点数和边数// char vexlist[MAX]; //顶点表// int vexlist_num[MAX]; //顶点代表// }; int main() { int i; ELGraph G; ENode e; FILE * fp; if ((fp = fopen("a.txt", "r")) == NULL) { //打开文件// printf("打开文件出错"); exit(0); } fscanf_s(fp, "%d%d", &G.vexnum,&G.edgenum); //读取顶点数和边数// for (i = 0; i < G.vexnum; i++) { fscanf_s(fp,"%s%d",G.vexlist[i],&G.vexlist_num[i]); //读取顶点表// } for (i = 0; i < G.edgenum; i++) { fscanf_s(fp,"%d%d%d", &e.ivex[i],&e.jvex[i],&e.weight[i]); } printf("%d",e.weight[4]); fclose(fp); return 0; }
好的,这段代码存在一些错误,我来帮你改正一下:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
struct ENode {
int ivex[MAX], jvex[MAX]; // 边依附的两个顶点
int weight[MAX]; // 边的权值
};
struct ELGraph {
int vexnum, edgenum; // 顶点数和边数
char vexlist[MAX][MAX]; // 顶点表
int vexlist_num[MAX]; // 顶点代表
};
int main() {
int i;
ELGraph G;
ENode e;
FILE* fp;
if ((fp = fopen("a.txt", "r")) == NULL) { // 打开文件
printf("打开文件出错");
exit(0);
}
fscanf(fp, "%d%d", &G.vexnum, &G.edgenum); // 读取顶点数和边数
for (i = 0; i < G.vexnum; i++) {
fscanf(fp, "%s%d", G.vexlist[i], &G.vexlist_num[i]); // 读取顶点表
}
for (i = 0; i < G.edgenum; i++) {
fscanf(fp, "%d%d%d", &e.ivex[i], &e.jvex[i], &e.weight[i]);
}
printf("%d", e.weight[4]); // 打印第五条边的权重
fclose(fp);
return 0;
}
```
主要问题有:
1. 结构体 `ELGraph` 中的 `vexlist` 应该定义为二维字符数组,因为每个顶点的名称可能包含多个字符。
2. 在读取顶点表时,应该使用 `G.vexlist[i]` 而不是 `&G.vexlist[i]`。
3. 在读取边的信息时,由于 `ivex`、`jvex` 和 `weight` 都是数组,应该使用下标 `i` 访问它们的元素,而不是使用 `&`。
4. 打印第五条边的权重时,应该使用 `e.weight[4]` 而不是 `G.weight[4]`。
fscanf(fp3, "%d", &conf.number); head->elem1 = conf.number; end = head; for (int i = 0; i < conf.number; i++) { //利用for循环向链表中添加数据 node = (struct Stu*)malloc(sizeof(struct Stu));//给普通节点申请内存空间 fscanf(fp3, "%d%d%d", &(node->elem1), &(node->elem2), &(node->elem3)); //给数据域赋值 end->next = node; //让上一个节点的数据域指向当前节点 end = node; //end指向当前节点,最终end指向尾节点 } end->next = NULL; print(head); //在屏幕上显示数据 return head; //返回头节点的地址什么意思
这段代码是一个函数,它的作用是从文件中读取数据,并将数据存储在一个链表中。具体来说,它首先从文件中读取一个整数,存储在conf.number中,然后利用for循环向链表中添加conf.number个节点,每个节点有三个整型元素elem1、elem2和elem3,这三个元素的值分别从文件中读取。最后,它将链表头节点的地址返回,并在屏幕上显示链表中的数据。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)