简体   繁体   English

从 'char *' 初始化 'char' 从指针生成整数而不进行强制转换

[英]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.

相关问题 警告:从 'uint8_t *' {aka 'unsigned char *'} 初始化 'unsigned char' 使得 integer 从没有强制转换的指针 [-Wint-conversion] - warning: initialization of 'unsigned char' from 'uint8_t *' {aka 'unsigned char *'} makes integer from pointer without a cast [-Wint-conversion] 数组初始化中“初始化不使用强制转换就从指针生成整数” - “initialization makes integer from pointer without a cast” waning in array initialization C 多维字符数组 - 赋值从指针生成整数而不进行强制转换 - C multidimentional char array - assignment makes integer from pointer without a cast “警告:赋值使 integer 从没有强制转换的指针”使用字符 malloc - “Warning: assignment makes integer from pointer without a cast” using a char malloc strcpy 从 integer 生成指针而不进行强制转换 - strcpy makes pointer from integer without a cast “赋值使 integer 从没有强制转换的指针”? - “assignment makes integer from pointer without a cast”? 警告:赋值使指针从整数开始,没有强制转换和排序参数 - warning: assignment makes integer from pointer without a cast, sorting arguments 运行代码时出错-使指针从整数开始而不进行强制转换 - Error while running code - makes pointer from integer without a cast 任何想法? 警告分配使来自 integer 的指针没有强制转换 - Any idea ? Warning assignment makes pointer from integer without a cast 警告:赋值可从指针生成整数,而无需在shellsort算法中强制转换 - Warning: assignment makes integer from pointer without a cast in shellsort algorithm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM