stu *head=(stu*)malloc(sizeof(stu));
时间: 2023-08-28 18:51:47 浏览: 49
这段代码是在动态内存中分配了一个大小为`sizeof(stu)`的内存块,并将其地址赋给了指针变量`head`。根据代码中的类型转换`(stu*)`,可以推测`stu`是一种结构体或者类型别名。
这段代码的作用是创建了一个链表的头节点,并且分配了足够的内存空间来存储该节点的数据。在实际使用链表时,可以通过`head`指针来访问和操作链表的头节点。需要注意的是,在使用完链表后,应该记得使用`free()`函数来释放动态分配的内存,以避免内存泄漏。
相关问题
c语言 Stu* stu = (Stu*)malloc(sizeof(Stu))是什么意思
这段代码是用来动态地分配内存空间,并返回一个指向分配的内存空间的指针。
具体地说,`malloc(sizeof(Stu))` 中的 `sizeof(Stu)` 表示 `Stu` 类型所占的内存空间大小,`malloc` 函数会在堆上分配这么大的连续内存空间,并返回分配的内存空间的首地址,也就是一个指向 `Stu` 类型内存空间的指针。然后,`(Stu*)` 是一个类型转换操作符,将 `malloc` 函数返回的 `void*` 类型指针转换为 `Stu*` 类型的指针,使得这个指针可以被赋值给 `Stu* stu` 变量。
最终,`Stu* stu = (Stu*)malloc(sizeof(Stu))` 的作用是动态分配一个 `Stu` 类型的内存空间,并返回一个指向这个内存空间的指针,这个指针可以被用来访问和操作这个内存空间。
修正如下代码:#include <stdio.h> #include <stdlib.h> #include <string.h> struct stu{ char name[20]; int no; struct stu *next; }; int myCreate(struct stu *head,int m); void show(struct stu *head); struct stu * myIndex(struct stu *head,char *s); int myDel(struct stu *head,char *s); int main() { struct stu head; struct stu *p; char name[20]; int m; scanf("%d", &m);// 输入m,代表结点个数 if(myCreate(&head,m)==1)//如果创建成功 { printf("created...\n"); show(&head);//显示所有结点的信息 } else printf("create failure."); return 0; } int myCreate(struct stu *head,int m) { struct stu *p,*pre; *head=NULL; while(m--) { (struct stu *) malloc(sizeof(struct stu)); scanf("%s %d",p->name,p->no); if(p->no==NULL) { return 0; } p->next = NULL; if (head == NULL) { head = p; } else { pre->next = p; } pre = p; } return 1; } void show(struct stu *head) { struct stu *p; *p=head->next; while(p!=NULL) { printf("%s %d\n",p->name,p->no); p=p->next; } }
好的,这里是修改后的代码:
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu{
char name[20];
int no;
struct stu *next;
};
int myCreate(struct stu **head,int m);
void show(struct stu *head);
struct stu * myIndex(struct stu *head,char *s);
int myDel(struct stu *head,char *s);
int main() {
struct stu *head = NULL;
char name[20];
int m;
scanf("%d", &m); // 输入m,代表结点个数
if(myCreate(&head,m)==1) { //如果创建成功
printf("created...\n");
show(head); //显示所有结点的信息
}
else {
printf("create failure.");
}
return 0;
}
int myCreate(struct stu **head,int m) {
struct stu *p,*pre;
*head = NULL;
while(m--) {
p = (struct stu *) malloc(sizeof(struct stu));
scanf("%s %d",p->name,&p->no);
if(p->no == 0) {
return 0;
}
p->next = NULL;
if (*head == NULL) {
*head = p;
}
else {
pre->next = p;
}
pre = p;
}
return 1;
}
void show(struct stu *head) {
struct stu *p;
p = head;
while(p != NULL) {
printf("%s %d\n",p->name,p->no);
p = p->next;
}
}
```
我对代码进行了如下修改:
1. 在 `myCreate` 函数中,将 `head` 的类型改为指向指针的指针 `struct stu **head`。这是因为我们要修改 `head` 的指向,如果直接传递 `struct stu *head`,那么在函数内部修改 `head` 的指向是无效的。
2. 在 `myCreate` 函数中,将 `p->no` 的判断条件改为 `p->no == 0`,因为 `p->no` 是一个整型变量,不能与 `NULL` 相比较。
3. 在 `myCreate` 函数中,将 `head` 的赋值改为 `*head = NULL`,因为 `head` 是指向指针的指针,需要使用 `*` 解引用,才能将 `NULL` 赋值给指针。
4. 在 `main` 函数中,将 `show(&head)` 改为 `show(head)`,因为 `head` 已经是一个指向结构体的指针了,不需要再取地址。
5. 在 `myCreate` 函数中,添加了一个 `pre` 指针,用于记录上一个结点的指针,以便将新结点插入到链表中。
6. 在 `show` 函数中,将 `*p=head->next` 改为 `p = head`,因为 `head` 已经是一个指向结构体的指针了,不需要再取地址。