1.定义车的结构体信息:品牌,单价、颜色,
1,定义函数实现结构体指针指向堆区空间
2.定义函数,实现结构体指针的输入
3.定义函数,实现结构体指针的输出
4.定义函数,释放堆区空间
#include "test1.h"
struct car* create()
{
struct car* p;
p=(struct car*)malloc(sizeof(struct car)*3);
if(p==NULL)
{
printf("malloc failed\n");
return NULL;
}
return p;
}
void car_input(struct car* a)
{
int i;
for(i=0;i<3;i++)
{
scanf("%s",(a+i)->brand);
getchar();
scanf("%c",&(a+i)->color);
scanf("%f",&(a+i)->money);
}
}
void out_put(struct car* a,int n)
{
int i;
printf("品牌\t颜色\t价格\n");
for(i=0;i<n;i++)
{
printf("%s\t%c\t%.2f\n",(a+i)->brand,(a+i)->color,(a+i)->money);
}
}
struct car* car_free(struct car* a)
{
if(a==NULL)
{
printf("a is NULL\n");
return NULL;
}
free(a);
a=NULL;
return a;
}
#ifndef ___HEAD_H__
#define ___HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#endif
struct car
{
char brand[20];
char color;
float money;
};
void out_put(struct car* a,int n);
void car_input(struct car* a);
struct car* create();
struct car* car_free(struct car* a);
#include "test1.h"
int main(int argc, const char *argv[])
{
struct car arr[3];
struct car* a;
a=create();
car_input(a);
out_put(a,3);
car_free(a);
a=NULL;
return 0;
}