作业:多文件编译
1.定义车的结构体信息:品牌,单价、颜色,
1,定义函数实现结构体指针指向堆区空间
2.定义函数,实现结构体指针的输入
3.定义函数,实现结构体指针的输出
4.定义函数,释放堆区空间
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct car
{
char name[10];
int piece;
char colour[10];
};
struct car * output();
struct car * free_1(struct car *p);
void input_1(struct car *p,int n);
void output_1(struct car *p,int n);
#endif
test.c
#include"head.h"
struct car * output()
{
struct car *p=(struct car *)malloc(sizeof(struct car)*3);
if(p == NULL)
{
return NULL;
}
return p;
}
struct car * free_1(struct car *p)
{
if(NULL == p)
{
return p;
}
free(p);
p = NULL;
return p;
}
void input_1(struct car *p,int n)
{
int i=0;
for(i=0 ; i<n ; i++)
{
scanf("%s",(p+i)->name);
scanf("%d",&(p+i)->piece);
scanf("%s",(p+i)->colour);
}
}
void output_1(struct car *p,int n)
{
for(int i=0 ; i<n ; i++)
{
printf("%s\t%d\t%s\n",(p+i)->name,(p+i)->piece,(p+i)->colour);
}
}
main.c
#include"head.h"
int main()
{
struct car *p=output();
input_1(p,3);
printf("品牌\t价格\t颜色\n");
output_1(p,3);
p=free_1(p);
return 0;
}