[英]initialization of ‘char’ from ‘char *’ makes integer from pointer without a cast
I'm trying to anonymously create a struct using a couple variables and gcc, given the flag -Werror
, won't compile the following:我正在尝试使用几个变量和 gcc 匿名创建一个结构,给定标志-Werror
,不会编译以下内容:
char file_name[A1FS_NAME_MAX];
strcpy(file_name, strrchr(path, '/') + 1);
a1fs_dentry *new_dentry_ptr = (a1fs_dentry *) fs->data_table + last_extent->start * A1FS_BLOCK_SIZE + remaining_directory_entries * sizeof(a1fs_dentry);
*new_dentry_ptr = (a1fs_dentry) {
.ino = (a1fs_ino_t) fs->first_free_inode_i,
.name = file_name
};
where a1fs_dentry
is defined as follows:其中a1fs_dentry
定义如下:
typedef struct a1fs_dentry {
/** Inode number. */
a1fs_ino_t ino;
/** File name. A null-terminated string. */
char name[A1FS_NAME_MAX];
} a1fs_dentry;
The warning causing it to fail is at .name = file_name
.导致它失败的警告位于.name = file_name
。 It also says error: missing braces around initializer
.它还说error: missing braces around initializer
。 I tried casting file_name to a char array but it doesn't like that.我尝试将 file_name 转换为 char 数组,但它不喜欢那样。 My goal is to get it to where it doesn't give those warnings anymore.我的目标是让它不再发出这些警告。
This initialization of the compound literal in the statement below is invalid下面语句中复合文字的这种初始化是无效的
*new_dentry_ptr = (a1fs_dentry) {
.ino = (a1fs_ino_t) fs->first_free_inode_i,
.name = file_name
};
You may not initialize the character array name
with the expression file_name
that is implicitly converted to a pointer when used as an initializer,您不能使用表达式file_name
初始化字符数组name
,该表达式在用作初始化程序时隐式转换为指针,
So instead of using the compound literal you should explicitly assign values to data members of the object pointed to by the pointer new_dentry_ptr
and for the data member name
use the standard string function strcpy
as for example因此,不要使用复合文字,您应该显式地为指针new_dentry_ptr
指向的对象的数据成员new_dentry_ptr
,对于数据成员name
,例如使用标准字符串函数strcpy
strcpy( new_dentry_ptr->name, file_name );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.