c语言指针和结构体例题
时间: 2023-07-09 09:12:33 浏览: 53
好的,这个问题应该比较简单。下面是一个例子:
```
#include <stdio.h>
// 定义一个结构体
struct Student {
int id;
char name[20];
int age;
};
int main() {
// 定义一个结构体变量
struct Student stu;
// 定义一个指向结构体的指针变量
struct Student *p;
// 将指针变量指向结构体变量
p = &stu;
// 通过指针操作结构体变量
p->id = 1001;
strcpy(p->name, "John");
p->age = 18;
// 输出结构体变量的值
printf("id: %d\n", stu.id);
printf("name: %s\n", stu.name);
printf("age: %d\n", stu.age);
return 0;
}
```
这个例子中,我们定义了一个结构体 `Student`,包含了学生的 ID、姓名和年龄。然后我们在 `main` 函数中定义了一个结构体变量 `stu` 和一个指向结构体的指针变量 `p`,并将指针变量指向结构体变量。接着,我们通过指针操作结构体变量,给它的成员赋值。最后,我们输出结构体变量的值,可以看到它的成员已经被赋值了。
希望这个例子能帮助你理解指针和结构体的用法。
相关问题
结构体指针 c语言例题
下面是一个结构体指针的 C 语言例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义一个结构体类型
struct student {
char name[20];
int age;
float score;
};
int main() {
// 动态分配结构体内存并初始化
struct student *s = (struct student *)malloc(sizeof(struct student));
strcpy(s->name, "Tom");
s->age = 18;
s->score = 89.5;
// 使用指针访问结构体成员并打印
printf("name: %s\n", s->name);
printf("age: %d\n", s->age);
printf("score: %.1f\n", s->score);
// 释放内存
free(s);
return 0;
}
```
这个例子定义了一个结构体类型 `struct student`,包含姓名、年龄和成绩三个成员。在主函数中,使用 `malloc` 函数动态分配了一个结构体内存,并使用指针访问结构体成员并打印输出。最后使用 `free` 函数释放了内存。
c语言双向循环链表例题
以下是一个C语言的双向循环链表的例题:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void insert(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
if (*head == NULL) {
newNode->next = newNode;
newNode->prev = newNode;
*head = newNode;
} else {
struct Node* tail = (*head)->prev;
newNode->next = *head;
(*head)->prev = newNode;
newNode->prev = tail;
tail->next = newNode;
}
}
void display(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
struct Node* head = NULL;
// 插入节点
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
// 显示链表
display(head);
return 0;
}
```
这个例题演示了如何创建并打印一个双向循环链表。首先,我们创建了一个结构体 `Node`,其中包含数据 `data`、下一个节点指针 `next` 和前一个节点指针 `prev`。然后,我们定义了一个插入函数 `insert`,用于将新节点插入到链表中。最后,我们定义了一个显示函数 `display`,用于打印链表中的所有节点。在 `main` 函数中,我们创建一个空链表,然后插入了三个节点,并使用 `display` 函数打印链表的内容。
这个例题只是对双向循环链表的基本操作进行了演示,你可以根据需要修改和扩展这个代码来实现更复杂的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [链表图解(双向、循环链表+链表增删)](https://blog.csdn.net/qq_52189899/article/details/121552785)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)