gpt4 book ai didi

c - 从 C 中的结构开始

转载 作者:行者123 更新时间:2023-11-30 19:09:27 24 4
gpt4 key购买 nike

每次我尝试编译以下代码时,都会遇到大量错误。有谁看到我在 C 程序中忽略的任何明显的事情吗?任何帮助,将不胜感激。还附上了头文件 Structures.h。

这是结构.cc 文件

#include <stdio.h>
#include <string.h>
#include "structures.h"

void getOnePerson(person_t *p) {
char *name;
char *ssn;
int *year;

printf("Enter full name: ");
scanf("%s", &name);
p.name = &name;
printf("\nEnter ssn: ");
scanf("%s", &ssn);
p.ssn = &ssn;
printf("\nEnter year of birth: ");
scanf("%d", year);
p.yearOfBirth = &year;

}

void printOnePerson(person_t t1) {
char *name = t1.name;
char *ssn = t1.ssn;
int year = t1.yearOfBirth;
printf("%s", &name);
printf(":");
printf("%s", &ssn);
printf(":");
printf("%p", &year);
printf("\n");

}

void getPeople(person_t peoples[], int people) {
for (int a = 0; a < people; a++) {
getOnePerson(peoples[a]);
}
}

void printPeople(person_t peoples[], int people) {
for (int p = 0; p < people; p++) {
peoples[p];
}
}

这是结构.h 文件

#include <stdio.h>
#include <string.h>

#define NAME_SIZE 80
#define SSN_SIZE 13
#define NUM_PEOPLE 10

typedef struct{
char name[NAME_SIZE];
char ssn[SSN_SIZE];
int yearOfBirth;
}person_t;

void getOnePerson(struct person_t *p);
void printOnePerson(struct person_t p);
void getPeople(struct person_t peoples[], int people);
void printPeople(struct person_t peoples[], int people);

最佳答案

头文件中的函数声明不正确。

您引用了一个或多个 struct person_t 类型的参数,但不存在这样的类型。有一个名为 person_t 的类型,因此更改函数原型(prototype)以使用它。

void getOnePerson(person_t *p);
void printOnePerson(person_t p);
void getPeople(person_t peoples[], int people);
void printPeople(person_t peoples[], int people);

您的获取和打印功能也有几个问题。在这两种情况下,您都使用中间变量来读取/写入不必要的值:

void getOnePerson(person_t *p) {

printf("Enter full name: ");
scanf("%s", p->name);
printf("\nEnter ssn: ");
scanf("%s", p->ssn);
printf("\nEnter year of birth: ");
scanf("%d", &p->yearOfBirth);

}

void printOnePerson(person_t t1) {

printf("%s", t1.name);
printf(":");
printf("%s", t1.ssn);
printf(":");
printf("%d", t1.yearOfBirth);
printf("\n");

}

关于c - 从 C 中的结构开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43244630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com