我这里的程序没有正确打印出来。这是输入:
有多少员工? 2
输入员工1信息:月/日/年,年龄,身高,姓名: 3/21 / 34,43,3.4,hdsfgdf
输入员工2信息:月/日/年,年龄,身高,姓名: 4/44 / 44,44,6.2,dfgtesas
这是输出:
员工1信息: 0 / -1081689528 / 134514548,16564212,0.0,
员工2信息: 0/1/14608664,-1217230008,0.0, ܹ
我唯一的猜测是我没有正确填充数组,或者我打印的是地址而不是数据。我是正确的假设吗?任何建议都会有所帮助。非常感谢!
我的代码有3个文件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "personal.c"
LIST *start, *end;
int main(void)
{
int i, numEmp;
PERSON person;
start=end=NULL;
printf("How many employees? ");
scanf("%d", &numEmp);
PERSON employees[numEmp];
for (i = 0; i < numEmp; i++)
{
printf("Enter employee %d info: month/day/year, age, height, name:\n", i+1);
scanf("%d/%d/%d,%d,%f,%s", &person.bday.month, &person.bday.day,
&person.bday.year, &person.age, &person.height, person.name);
add(&start, &end, person);
}
for (i = 0; i < numEmp; i++)
{
printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", i+1, employees[i].bday.month, employees[i].bday.day, employees[i].bday.year, employees[i].age, employees[i].height, employees[i].name);
delete(&start, &end);
}
#ifndef LIST_H_ /* to prevent re-definitions */
#define LIST_H_ /* that cause errors */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct DATE
{
int month;
int day;
int year;
} DATE;
typedef struct PERSON
{
char name[41];
int age;
float height;
DATE bday;
} PERSON;
typedef struct list
{
PERSON data;
struct list *next;
} LIST;
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
int delete (LIST **head, LIST **tail){
LIST *temp;
if (*head == NULL)
return -1;
PERSON retVal = (*head)->data;
if(*head==*tail){
free(*head);
*head=*tail=NULL;
}
else{
temp=(*head)->next;
free(*head);
*head=temp;
}
//return retVal;
}
void add(LIST **head, LIST **tail, PERSON data){
if(*tail==NULL){
*head=*tail=(LIST *) malloc(sizeof(LIST));
(*head)->data=data;//use arrow when struct is pointer. Use . if have direct access to struct
(*head)->next=NULL;
}
else{
(*tail)->next= (LIST *) malloc(sizeof(LIST));
*tail=(*tail)->next;
(*tail)->data=data;
(*tail)->next=NULL;
}
}
答案 0 :(得分:2)
printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", i+1,
employees[i].bday.month, employees[i].bday.day, employees[i].bday.year,
employees[i].age, employees[i].height, employees[i].name);
您正在尝试打印从未初始化的employees
数组。
答案 1 :(得分:1)
在valgrind下运行您的程序。它是一个免费的工具,可以自动检测代码中的内存错误。它可能会突出显示您的代码正在读取或写入无效内容的位置。
所有你要做的就是在你面前运行你的程序valgrind
,假设你在Linux系统上并安装了valgrind软件包。
答案 2 :(得分:1)
您可以创建一个可变长度数组(employees
)和链接列表。您可以向链接列表添加元素,但尝试打印可变长度数组的内容(您从未写过任何内容)。
或者:
将数据读入您的VLA。
for (i = 0; i < numEmp; i++)
{
printf("Enter employee %d info: month/day/year, age, height, name:\n", i+1);
scanf("%d/%d/%d,%d,%f,%s", &employees[i].bday.month, &employees[i].bday.day,
&employees[i].bday.year, &employees[i].age, &employees[i].height, employees[i].name);
}
忘记VLA并仅打印链接列表的内容。
i = 0;
for (LIST *item = start; item != NULL; item = item->next)
printf("Employee %d information:\n%d/%d/%d, %d, %.1f, %s\n", ++i, item->data.bday.month, item->data.bday.day, item->data.bday.year, item->data.age, item->data.height, item->data.name);