struct.c

来自「linux下网络编程的函数」· C语言 代码 · 共 47 行

C
47
字号
#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct BIO{char name[16];unsigned age;}BIO;BIO *CreateBio(const char *name, unsigned age);int main(void){BIO exfriend;BIO *friend;friend = CreateBio("Sarah",16);if(friend != NULL)printf("My friend's name is %s\n""and my friend's age is %u\n",friend->name,friend->age);else puts("Failed to create Bio");if(friend != NULL){exfriend = *friend;printf("\nMy former friend's name is %s\n""and my former friend's age is %u\n",exfriend.name, exfriend.age);}free(friend);return 0;}BIO *CreateBio(const char *name, unsigned age){BIO *tmp = malloc(sizeof *tmp);if(tmp != NULL){unsigned i = sizeof(tmp->name);tmp->age = age;strncpy(tmp->name,name,i);tmp->name[i-1] = '\0';}return tmp;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?