Home > Back-end >  initialization of ‘char’ from ‘char *’ makes integer from pointer without a cast
initialization of ‘char’ from ‘char *’ makes integer from pointer without a cast

Time:10-13

I'm trying to anonymously create a struct using a couple variables and gcc, given the flag -Werror, won't compile the following:

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:

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. It also says error: missing braces around initializer. I tried casting file_name to a char array but it doesn't like that. My goal is to get it to where it doesn't give those warnings anymore.

CodePudding user response:

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,

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

strcpy( new_dentry_ptr->name, file_name );
  • Related