案例一:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
struct Dog
{char *name;int age;char weight;
};
char *get_dog_info(struct Dog dog)
{static char info[40];sprintf(info, "name:%s, age:%d, weight:%d", dog.name, dog.age, dog.weight);return info;
};char *get_dog_info1(struct Dog *dog)
{static char info1[30];sprintf(info1, "name:%s, age:%d, weight:%d", dog->name, dog->age, dog->weight);return info1;
};int main()
{struct Dog dog = {"旺財", 2, 10};printf("dog info: %s\n", get_dog_info(dog));struct Dog *dog1 = &dog;char *Info = NULL;Info = get_dog_info1(dog1);printf("dog1 info: %s\n", Info);printf("dog1 info: %s\n", get_dog_info1(dog1));return 0;
}
案例二:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
struct Box
{double length;double width;double height;
};double bulk(struct Box *box)
{return box->length * box->width * box->height;
}int main()
{struct Box box1;printf("請輸入長、寬、高:\n");scanf("%lf %lf %lf", &box1.length, &box1.width, &box1.height);printf("體積 = %.2lf\n", bulk(&box1)); return 0;
}
案例三:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
struct Person
{char name[20];int age;double pay;
};void print(struct Person *ptr)
{ptr->pay =( ptr->age > 18) ? 3000 : 1000;
}int main()
{struct Person ptr11;printf("請輸入姓名、年齡\n");scanf("%s %d", &ptr11.name, &ptr11.age);print(&ptr11);struct Person *ptr = &ptr11;printf("姓名:%s ,年齡:%d ,工資:%.2lf \n", ptr->name, ptr->age, ptr->pay); return 0;
}