继续昨天的内容,把结构体剩下的一点内容写完。
struct Man{
char *name;
int age;
}
void main(){
//malloc 返回值默认是void* 所以我们最好写成我们自己的类型
struct Man *m_p = (struct Man*)malloc(sizeof(struct Man) * 10);
struct Man *p = m_p;
//赋值
p->name = "Tom";
p->age = 10;
p++;
p->name = "Jerry";
p->age = 11;
struct Man *loop_p = m_p;
for(; loop_p <m_p +2;loop_p++){
printf("%s,%d\n",loop_p->name,loop_p->age);
}
free(m_p);
getchar();
}
struct Man{
char name [20];
int age;
};
//Age是int类型的别名
typedef int Age;
//Ap是int类型指针的别名
typedef int * Ap;
typedef struct Man JavaMan;
typedef struct Man * Jn;
//结构体取别名
typedef struct Woman{
char name[20];
int age;
}W,*WP;//W是woman结构体的别名WP是woman结构体指针的别名
void main (){
int i =5;
Ap p = &i;
//结构体变量
W w1 = {"Rose",20};
//结构体指针
WP wp1 = &w1;
printf("%s,%d\n",w1.name,w1.age);
printf("%s,%d\n",wp1->name,wp1->age);
getchar();
}
struct Girl{
char name[20];
int age;
//函数指针
void (*sayHi)(char*);
};
//Girl 结构体类似与Java中的类,name和age类似于属性,sayHi类似于方法
void sayHi(char*text){
MessageBoxA(0,"Hi","title",0);
}
void main(){
struct Girl g1;
g1.name = "Lucy";
g1.sayHi = sayHi;
g1.sayHi("hello");
getchar();
}
typedef struct Girl{
char name[20];
int age;
//函数指针
void (*sayHi)(char*);
}Girl;
//Girl 结构体类似与Java中的类,name和age类似于属性,sayHi类似于方法
void sayHi(char*text){
MessageBoxA(0,"Hi","title",0);
}
//Girl结构体指针取别名GirlP
typedef Girl* GirlP;
//改名
void rename(GirlP gp1){
gp1 -> name = "Lily";
}
void main(){
Girl g1 = {"Lucy",18,sayHi};
GirlP gp1 = &g1;
gp1->sayHi("Byebye!");
//传递指针,改名
rename(gp1);
getchar();
}
char a[10] = "Happy";
//a = "Sad";不行
//可以修改内容
a[0]= "F";
//重新赋值
strcpy(a,"Sad");
//字符指针,可以多次赋值不同字符串
char *b = "Friend";
b = "Family";
b++;
//*b = 'H';//不能修改字符内容
联合体
,欲速则不达,慢慢来不着急。