⑥NDK学习之C语言结构体struct例子

来源:互联网 发布:淘宝精装修教程pdf 编辑:程序博客网 时间:2024/05/21 11:36



1
#include<stdlib.h>
2
#include<stdio.h>
3
#include<string.h>
4
#include<Windows.h>
5
6
//定义一个Girl结构体,包括属性和方法
7
typedef struct Girl {
8
    char* name;
9
    int age;
10
    //函数指针
11
    void(*sayHi)(char*);
12
}Girl;//给结构体取了一个别名Girl(别名可以与结构体原来的名字相同)
13
14
//Girl结构体指针取别名GirlP
15
typedef Girl* GirlP;
16
17
//结构体的成员函数
18
void sayHi(char* text) {
19
    MessageBoxA(0,text,"title",0);
20
}
21
//自定义一个函数
22
void renames(GirlP gp1) {
23
    gp1->name = "Lily";
24
}
25
26
void main() {
27
    Girl g1 = {"Lucy",18,sayHi};
28
    printf("我的名字:%s",g1.name);
29
    GirlP gp1 = &g1;
30
    gp1->sayHi("Byebye:");
31
    //传递指针,改名(只有传递指针才能修改值,所以指针是比较常用的方式)
32
    renames(gp1);
33
34
    getchar();
35
}

原创粉丝点击