1.首先就是结构体的相关定义
struct student//结构体名称
{
int num; //各种元素
char name[];
char inpone[];
} stu;//变量名
关于结构体的声明
struct student//结构体名称
{
int num; //各种元素
char name[];
char inpone[];
} ;//变量名
这样用下面声明也可以
struct student stu; //引用前的声明 在初始化时变量名可以不写 ,
//但是在声明时必须写
结构体内部的引用
c = stu.num;
printf("%s",stu.name);
2.关于结构体做为函数参数
#include <stdio.h>
#include <string.h>
struct student
{
int age;
char name[20];
} ;
void print (struct student);
int main ()
{
struct student stu;
stu.age = 20;
//stu.name = "sunchao";这样赋值显示错误 会显示定义有20个字符但是只用了8个
strcpy(stu.name,"sunchao");//了解一下strcpy的使用方法
print(stu);
}
void print( struct student stu)
{
printf("%d",stu.age);
printf("%s",stu.name);
}
3.用结构指针变量作为函数参数
//用结构体变量 当函数参数
#include <stdio.h>
#include <string.h>
//用结构体指针做函数参数
struct student
{
int age;
char name[20];
} ;
void print (struct student *);
int main ()
{
struct student stu;
stu.age = 20;
//stu.name = "sunchao";这样赋值显示错误
strcpy(stu.name,"sunchao");//了解一下strcpy的使用方法
print(&stu);
}
void print( struct student *p)
{
printf("%d\n",(*p).age);
printf("%s\n",(*p).name);
printf("%d\n",p->age);
printf("%s\n",p->name);
}