[toc]

c语言中结构体的使用

1定义结构体

1
2
3
4
5
6
7
struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}_Process;

2使用结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
};

int main(int argv, char* argc[])
{
struct Process Info = {0};
Info.name = L"cmd.exe";
wprintf(L"name is %s\n", Info.name);
return 0;
}

3定义结构体的同时赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}Info = {L"cmd.exe",9523};

int main(int argv,char*argc[])
{
wprintf(L"name is %s\n", Info.name);
wprintf(L"pid is %d\n", Info.pid);
return 0;
}

4使用typedef定义结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
typedef struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}_Process;

int main(int argv, char* argc[])
{
struct Process Info;
Info.name = L"cmd.exe";
wprintf(L"Info.name is %s\n", Info.name);

_Process _Info;
_Info.name = L"cmd.exe";
wprintf(L"_Info.name is %s\n", _Info.name);
return 0;
}

5将结构体作为函数参数传入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
typedef struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}_Process;
void PrintfMember(_Process Info)
{
wprintf(L"Info.name is %s\n", Info.name);
}
int main(int argv, char* argc[])
{
_Process Info;
Info.name = L"cmd.exe";
PrintfMember(Info);
return 0;
}

c语言中指针结构体的使用

1.使用结构体类型的指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
typedef struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
};

int main(int argv, char* argc[])
{
struct Process Info;
struct Process* pInfo=&Info;
pInfo->name = L"cmd.exe";
wprintf(L"Info.name is %s\n", pInfo->name);
wprintf(L"Info.name is %s\n", Info.name);
return 0;
}

2将结构体地址作为函数参数传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
typedef struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}_Process;
void PrintfMember(struct Process* Info)
{
wprintf(L"Info.name is %s\n", Info->name);

}
int main(int argv, char* argc[])
{
struct Process Info;
Info.name = L"cmd.exe";
PrintfMember(&Info);
wprintf(L"Info.name is %s\n", Info.name);
return 0;
}

3使用malloc给结构体分配内存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <malloc.h>
typedef struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}_Process, *PtrProcess;

int main(int argv, char* argc[])
{
PtrProcess pInfo;
pInfo = malloc(sizeof(_Process));
pInfo->name = L"cmd.exe";
pInfo->pid = 9523;
pInfo->ppid = 1024;
pInfo->path = L"C://Windows//System32/cmd.exe";
wprintf(L"Info.name is %s\n", pInfo->name);
wprintf(L"Info.name is %d\n", pInfo->pid);
wprintf(L"Info.name is %d\n", pInfo->ppid);
wprintf(L"Info.name is %s\n", pInfo->path);
free(pInfo);
pInfo = (void*)0;
return 0;
}

4.将结构体指针作为函数参数传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
typedef struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}_Process, *PtrProcess;
void PrintfMember(PtrProcess pInfo)
{
wprintf(L"Info.name is %s\n", pInfo->name);
wprintf(L"Info.name is %d\n", pInfo->pid);
wprintf(L"Info.name is %d\n", pInfo->ppid);
wprintf(L"Info.name is %s\n", pInfo->path);

}
int main(int argv, char* argc[])
{
PtrProcess pInfo = (PtrProcess)malloc(sizeof(_Process));
assert(pInfo != NULL);
pInfo->name = L"cmd.exe";
pInfo->pid = 9523;
pInfo->ppid = 1024;
pInfo->path = L"C://Windows//System32/cmd.exe";
PrintfMember(pInfo);
free(pInfo);
pInfo = (void*)0;
return 0;
}

5.通过空指针将结构体指针作为函数参数传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
typedef struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}_Process, *PtrProcess;
void PrintfMember(void* addr)
{
PtrProcess pInfo= (PtrProcess)addr;
assert(pInfo != NULL);
wprintf(L"Info.name is %s\n", pInfo->name);
wprintf(L"Info.name is %d\n", pInfo->pid);
wprintf(L"Info.name is %d\n", pInfo->ppid);
wprintf(L"Info.name is %s\n", pInfo->path);

}
int main(int argv, char* argc[])
{
PtrProcess pInfo = (PtrProcess)malloc(sizeof(_Process));
assert(pInfo != NULL);
pInfo->name = L"cmd.exe";
pInfo->pid = 9523;
pInfo->ppid = 1024;
pInfo->path = L"C://Windows//System32/cmd.exe";
PrintfMember(pInfo);
free(pInfo);
pInfo = (void*)0;
return 0;
}

6.通过空指针将结构体指针作为函数参数传递赋值后再传回来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
typedef struct Process
{
wchar_t* name;
int pid;
int ppid;
wchar_t* path;
}_Process, *PtrProcess;
void PrintfMember(void* addr)
{
PtrProcess pInfo= (PtrProcess)addr;
assert(pInfo != NULL);
pInfo->name = L"cmd.exe";
pInfo->pid = 9523;
pInfo->ppid = 1024;
pInfo->path = L"C://Windows//System32/cmd.exe";

}
int main(int argv, char* argc[])
{
PtrProcess pInfo = (PtrProcess)malloc(sizeof(_Process));
assert(pInfo != NULL);
PrintfMember(pInfo);
wprintf(L"Info.name is %s\n", pInfo->name);
wprintf(L"Info.name is %d\n", pInfo->pid);
wprintf(L"Info.name is %d\n", pInfo->ppid);
wprintf(L"Info.name is %s\n", pInfo->path);
free(pInfo);
pInfo = (void*)0;
return 0;
}

6.指针类型作为结构体的成员变量使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
typedef struct Process
{
wchar_t** name;
int* pid;
int* ppid;
wchar_t** path;
}_Process, *PtrProcess;
void PrintfMember(void* addr)
{
PtrProcess pInfo = (PtrProcess)addr;
assert(pInfo != NULL);
pInfo->name[0] = (wchar_t*)L"cmd.exe";
pInfo->pid[0] = 9523;
pInfo->ppid[1] = 1024;
pInfo->path[1] = (wchar_t*)L"C://Windows//System32/cmd.exe";

}
int main(int argv, char* argc[])
{
PtrProcess pInfo = (PtrProcess)malloc(sizeof(_Process));
assert(pInfo != NULL);
pInfo->name = (wchar_t**)malloc(sizeof(wchar_t));
pInfo->pid = (int*)malloc(sizeof(int));
pInfo->ppid = (int*)malloc(sizeof(int));
pInfo->path = (wchar_t**)malloc(sizeof(wchar_t));

PrintfMember(pInfo);
wprintf(L"Info.name is %s\n", pInfo->name[0]);
wprintf(L"Info.name is %d\n", pInfo->pid[0]);
wprintf(L"Info.name is %d\n", *(pInfo->ppid+1));
wprintf(L"Info.name is %s\n", *(pInfo->path+1));
free(pInfo);
pInfo = (void*)0;
return 0;
}