Home > database >  Adding struct value to char array in C
Adding struct value to char array in C

Time:12-15

I have some value. Say it's

dir->d_name;

I want to add this value to char array

char fileNames[100];

I created a temporary char value trying to assign dir->d_name

char temp_fileName;
temp_fileName = dir->d_name;

How can I do that or maybe I should convert dir->d_name to char or string?

Warning: assignment to 'char' from 'char*' makes integer from pointer without a cast

CodePudding user response:

If you want to store the reference (pointer):

char *temp_fileName;
temp_fileName = dir->d_name;

If you want to store the copy of the string:

char *temp_fileName = strdup(dir->d_name);

or

char *temp_fileName = malloc(strlen(dir ->d_name) 1);
if(temp_fileName) strcpy(temp_fileName, dir ->d_name);

or if temp_fileName is an automatic variable

char temp_fileName[strlen(dir ->d_name) 1];
strcpy(temp_fileName, dir ->d_name);

CodePudding user response:

As the warning says, this is a char* (char pointer), not a char:

char* temp_fileName;
/*  ^-- Here */
temp_fileName = dir->d_name;
  • Related