Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C结构中奇怪的常量字符指针成员初始化_C - Fatal编程技术网

C结构中奇怪的常量字符指针成员初始化

C结构中奇怪的常量字符指针成员初始化,c,C,在库示例中,有一个类型为struct lws\u http\u mountinitialized的变量。声明了stuct lws\u http\u挂载。为了方便起见,下面是其声明和定义的片段 struct lws_http_mount { const struct lws_http_mount *mount_next; const char *mountpoint; const char *origin; const char *def; const char *p

在库示例中,有一个类型为
struct lws\u http\u mount
initialized的变量。声明了
stuct lws\u http\u挂载。为了方便起见,下面是其声明和定义的片段

struct lws_http_mount {
   const struct lws_http_mount *mount_next;
   const char *mountpoint;
   const char *origin;
   const char *def;
   const char *protocol;
   const struct lws_protocol_vhost_options *cgienv;
   const struct lws_protocol_vhost_options *extra_mimetypes;
   const struct lws_protocol_vhost_options *interpret;
   int cgi_timeout;
   int cache_max_age;
   unsigned int auth_mask;
   unsigned int cache_reusable:1;
   unsigned int cache_revalidate:1;
   unsigned int cache_intermediaries:1;
   unsigned char origin_protocol;
   unsigned char mountpoint_len;
   const char *basic_auth_login_file;
   void *_unused[2];
};

static const struct lws_http_mount mount_localhost1 = {
   /* .mount_next */           NULL,
   /* .mountpoint */           "/",
   /* .origin */               "./mount-origin-localhost1",
   /* .def */                  "index.html",
   /* .protocol */              NULL,
   /* .cgienv */                NULL,
   /* .extra_mimetypes */       NULL,
   /* .interpret */             NULL,
   /* .cgi_timeout */           0,
   /* .cache_max_age */         0,
   /* .auth_mask */             0,
   /* .cache_reusable */        0,
   /* .cache_revalidate */      0,
   /* .cache_intermediaries */  0,
   /* .origin_protocol */       LWSMPRO_FILE,
   /* .mountpoint_len */        1,
   /* .basic_auth_login_file */ NULL,
}
此结构的成员
mountpoint
的类型为
const char*
。这已在变量
mount\u localhost1
中初始化为
“\”
。此结构内部为其成员
装入点
分配的实际字符数组大小是多少

我只知道结构中的char数组成员声明应该作为
char mountpoint[string\u length]
而不是
const char*mountpoint
在内存中创建字符串文本(char数组)
“/”
,常量char指针
装入点
用该内存的地址初始化

没有“在此结构内分配的实际字符数组大小”

这与初始化局部变量没有什么不同,例如:

const char*mountpoint=“/”;

变量(const char*)的大小只是指针的大小。实际的字符串(字符数组)与字符指针本身无关。

这正是我所期望的。“隐藏在内存中”似乎不是“其他地方”的好说法。它很可能位于支持这种东西的体系结构上的只读数据段中。更新了答案。