2

Whenever I try to use strcpy on a struct member (which is a static char array) and the d_name attribute of a dirent structure (used for reading a directory), my program doesn't get passed that point. I don't get any errors or warnings, it just doesn't get passed it. The struct is initialized globally.

typedef struct SearchArgs
{
    char inputFile[MAXDIRPATH];
    char word[MAXKEYWORD];
    Buffer * buffer;
}SearchArgs;

SearchArgs * args;//arguments for seachFile

...

dir = opendir(nextItem.path);//opens the next directory

...

dp = readdir(dir);

...

printf("dname: %s\n", dp->d_name);//prints
printf("args->inputFile: %s\n", args->inputFile);//prints
strcpy(args->inputFile, dp->d_name);//not getting passed this point
printf("TEST1\n");//doesn't print

Output:

dname: file2.txt
args->inputFile: (null)

Any insight is much appreciated.

Spencer Goff
  • 1,036
  • 3
  • 14
  • 23

1 Answers1

2

Before using strcpy (and any other opeation with structs or struct properties) it is necesarry to allocate memory space for that struct:

SearchArgs *args = (SearchArgs*)malloc(sizeof(SearchArgs));
user7771338
  • 185
  • 6