Hi all.
I am trying to compile the below program and getting these errors:
Code:
|In function ‘main’:|
18|error: incompatible types when assigning to type ‘char *[30]’ from type ‘char *’|
19|warning: passing argument 1 of ‘strcpy’ from incompatible pointer type [enabled by default]|
/usr/include/string.h|128|note: expected ‘char * __restrict__’ but argument is of type ‘char **’|
20|error: lvalue required as increment operand|
||=== Build finished: 3 errors, 1 warnings ===|
Why are the types incompatible with malloc? filename == filename[0]? So what is the problem? My reference books has not provided any solutions. Thanks.
Code:
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
DIR *dirname;
struct dirent *direntries;
char *filename[30];
dirname = opendir("/home/test/Downloads");
if(dirname != NULL)
{
while((direntries = readdir(dirname)))
{
filename = (char *) malloc(strlen(direntries->d_name));
strcpy(filename, direntries->d_name);
filename++;
}
(void) closedir(dirname);
}
else
{
perror("Error opening directory");
}
return 0;
}