将cin >> G.vexs[i].info;变成c语言
时间: 2024-05-15 07:13:06 浏览: 19
在 C 语言中,可以使用 scanf 函数来读入输入。将 "cin >> G.vexs[i].info;" 转换为 C 语言代码如下:
```
scanf("%s", G.vexs[i].info);
```
其中,"%s" 表示读入一个字符串。注意,在 C 语言中,字符串是以 '\0' 结尾的字符数组,所以需要保证 G.vexs[i].info 的空间足够存储输入的字符串。
相关问题
int InsertVex(AMGragh &G) {//在以邻接矩阵形式存储的无向图G上插入顶点 /**************begin************/ if(G.vexnum+1>MVNum)return error; int x; cin>>x; G.vexnum++; G.vexs[0][G.vexnum]=G.vexs[G.vexnum][0]=x; for(int i=1;i<=G.vexnum;i++) G.vexs[G.vexnum][i]=G.vexs[i][G.vexnum]=0; return ok;改正代码
int InsertVex(AMGraph &G) {
if(G.vexnum+1 > MAXVEX) return ERROR; // 判断是否超过最大顶点数
int x;
cin >> x;
G.vexnum++;
G.vexs[G.vexnum-1] = x; // 直接将新顶点存入末尾
for(int i = 0; i < G.vexnum-1; i++) {
G.arcs[i][G.vexnum-1] = G.arcs[G.vexnum-1][i] = 0; // 新顶点与其他点之间的边权值初始化为0
}
return OK;
}
G.vexs[i]= (VerTexType)malloc(sizeof(VerTexType));
在这段代码中,G.vexs[i]的类型是VerTexType,是一个结构体或基本数据类型,而malloc函数的返回值是void指针,需要将其转换为VerTexType指针才能赋值给G.vexs[i]。
因此,代码中的 `(VerTexType)` 就是将void指针转换为VerTexType指针的强制类型转换。然后,`malloc(sizeof(VerTexType))` 分配了一块大小为VerTexType的空间,并将其地址转换为VerTexType指针,赋值给了G.vexs[i]。
具体来说,这段代码是在为一个图的顶点分配空间。G.vexs是一个指针数组,每个元素都指向一个顶点结构体。因此,对于每个顶点,我们需要分配一定的空间来存储它的数据。这里使用了动态内存分配函数malloc来分配空间,而强制类型转换将其转换为正确的指针类型,以便将其赋值给G.vexs[i]。
需要注意的是,使用malloc分配的空间需要手动释放,否则会导致内存泄漏。因此,在程序结束时,需要使用free函数释放G.vexs中所有元素所分配的空间。例如:
```c
for (int i = 0; i < G.vexnum; i++) {
free(G.vexs[i]);
}
```
这样就可以释放所有顶点所占用的空间了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)