|
1鱼币
代码调了好几天调不出来,主要有以下几个问题
1、用结构体数组的话在打印时会出现访问冲突(如下代码)
2、用链表的话,最后打印出来的class和occupation为符号和数字(即不正确)
- #include<stdio.h>
- #include<stdlib.h>
- #include<String.h>
- #define NUM 2
- #define LEN sizeof(struct Info)
- #define N 100
- #define O 4
- //学生基本信息
- typedef struct Info{
- int num;
- char *name;
- char sex;
- char job;
- union {
- int class;
- char occupation[O];
- };
- }temp;
- struct Info input(temp Information);
- void print(struct Info p[]);
- int main() {
- struct Info Information[NUM];
- for (int i = 0; i < NUM; i++)
- {
- input(Information[i]);
- }
- print(Information);
- }
- //输入学生基本信息
- struct Info input(temp Information){
- char str[N];
- printf("Please give me the num:");
- scanf("%d", &Information.num);
- getchar();
- printf("Please give me the name:");
- gets(str);
- Information.name = (char*)malloc(sizeof(str) + 1);
- strcpy(Information.name, str);
- printf("Please give me the sex(M/F):");
- scanf_s("%c", &Information.sex);
- getchar();
- printf("Please give me the job(s/t):");
- scanf("%c", &Information.job);
- if (Information.job == 's') {
- printf("Please give me the class:");
- scanf("%d", &Information.class);
- }
- else if (Information.job == 't') {
- printf("Please give me the occupation:");
- char str2[O];
- getchar();
- gets(str2);
- strcpy(Information.occupation, str2);
- }
- else {
- printf("输入错!");
- return;
- }
- printf("\n");
- }
- //打印学生基本信息
- void print(struct Info p[]) {
- printf("num\tname\tsex\tjob\tclass/occupation\n");
- int i = 0;
- while (i < NUM) {
- if (p[i].job == 't') {
- printf("%d\t%s\t%c\t%c\t%d\n", p[i].num, p[i].name, p[i].sex, p[i].job, p[i].class);
- }
- else {
- printf("%d\t%s\t%c\t%c\t", p[i].num, p[i].name, p[i].sex, p[i].job);
- puts(p[i].occupation);
- }
- i++;
- }
- }
复制代码
以下是对您的代码出现问题的分析和修改建议:
1. **访问冲突问题:**
- 访问冲突可能是由于在输入和操作字符数组时,您使用 `char *` 而没有正确地分配内存或处理字符串。
- 在 `input` 函数中, `str` 是一个单字符变量,而不是字符串数组。应使用字符数组来读取和存储字符串。
2. **链表中的错误数据:**
- 在访问和使用字符串或未正确初始化的结构时可能会出现此类问题。
以下是修正后的代码:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define NUM 2
- #define N 100
- typedef struct Info{
- int num;
- char name[N];
- char sex;
- char job;
- union {
- int class;
- char occupation[N];
- };
- } Info;
- void input(Info *information);
- void print(Info p[], int n);
- int main() {
- Info information[NUM];
- for (int i = 0; i < NUM; i++) {
- input(&information[i]);
- }
- print(information, NUM);
- return 0;
- }
- //输入学生基本信息
- void input(Info *information){
- printf("Please give me the num: ");
- scanf("%d", &information->num);
- getchar();
- printf("Please give me the name: ");
- fgets(information->name, N, stdin);
- information->name[strcspn(information->name, "\n")] = '\0'; // Remove newline
- printf("Please give me the sex(M/F): ");
- scanf("%c", &information->sex);
- getchar();
- printf("Please give me the job (s/t): ");
- scanf("%c", &information->job);
- if (information->job == 's') {
- printf("Please give me the class: ");
- scanf("%d", &information->class);
- getchar();
- }
- else if (information->job == 't') {
- printf("Please give me the occupation: ");
- getchar();
- fgets(information->occupation, N, stdin);
- information->occupation[strcspn(information->occupation, "\n")] = '\0'; // Remove newline
- }
- else {
- printf("输入错误!\n");
- }
- printf("\n");
- }
- //打印学生基本信息
- void print(Info p[], int n) {
- printf("num\tname\tsex\tjob\tclass/occupation\n");
- for (int i = 0; i < n; i++) {
- if (p[i].job == 't') {
- printf("%d\t%s\t%c\t%c\t%s\n", p[i].num, p[i].name, p[i].sex, p[i].job, p[i].occupation);
- } else {
- printf("%d\t%s\t%c\t%c\t%d\n", p[i].num, p[i].name, p[i].sex, p[i].job, p[i].class);
- }
- }
- }
复制代码
**修改说明:**
- 使用字符数组 `char name[N]` 和 `char occupation[N]` 以避免动态分配字符串引起的错误。
- `fgets()` 用于读取字符串,以防止缓冲区溢出,并除去换行符。
- **建议:** 在遇到涉及动态内存和字符串操作时,小心使用指针和内存管理,以避免常见错误。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
最佳答案
查看完整内容
以下是对您的代码出现问题的分析和修改建议:
1. **访问冲突问题:**
- 访问冲突可能是由于在输入和操作字符数组时,您使用 `char *` 而没有正确地分配内存或处理字符串。
- 在 `input` 函数中, `str` 是一个单字符变量,而不是字符串数组。应使用字符数组来读取和存储字符串。
2. **链表中的错误数据:**
- 在访问和使用字符串或未正确初始化的结构时可能会出现此类问题。
以下是修正后的代码:
** ...
|