typedef struct {
char *p_end; /* pointer to NULL at end of path */
char *target_end; /* pointer to end of target base */
char p_path[PATH_MAX]; /* pointer to the start of a path */
} PATH_T;
static char emptystring[] = "";
PATH_T to = { to.p_path, emptystring, "" };
1
knva 2017-09-06 10:34:48 +08:00
百度 typedef
|
2
deweixu OP |
3
Shura 2017-09-06 11:13:00 +08:00 via Android 1
@deweixu to 是 PATH_T 类型,`PATH_T to = { to.p_path, emptystring, "" };`是给 to 整体赋值,to.p_path 为数组的首元素的地址,赋值给 tp.p_end。
|
4
suikator 2017-09-06 11:21:16 +08:00 via Android
C99 标准?
|
6
feiyuanqiu 2017-09-06 11:24:22 +08:00
我估计楼主想问的是为什么可以用 to.p_path 初始化 to.p_end
因为 to.p_path 在 to 声明时就分配了内存空间 |
8
zergmk2 2017-09-06 11:30:25 +08:00
这个是 GCC 初始化结构体的一种方法
PATH_T to = { to.p_path, emptystring, "" }; 相当于 PATH_T to = { .p_end = to.p_path, .target_end = emptystring, .p_path = "" }; |
9
z0z 2017-09-06 11:52:14 +08:00
如果有条件的话,楼主在机器上试一下就知道了,估计是在面试?
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 #define PATH_MAX (8) 6 7 typedef struct 8 { 9 char *p_end; 10 char *target_end; 11 char p_path[PATH_MAX]; 12 }PATH_T; 13 14 static char emptystring[]=""; 15 PATH_T to = {to.p_path,emptystring,""}; 16 17 int main(void) 18 { 19 printf("emptystring=0x%x\n",emptystring); 20 printf("to.p_end=[0x%x],to.target_end=[0x%x],to.p_path=[0x%x]\n",to.p_end,to.target_end,to.p_path); 21 22 23 return 0; 24 } xx@server:~/temp/testtype$ ./test emptystring=0x601068 to.p_end=[0x601050],to.target_end=[0x601068],to.p_path=[0x601050] xx@server:~/temp/testtype$ |
10
deweixu OP |