I am trying to access a const char* created in function1 in function2 in order to use it for some operations. It has to be in a different function for several reasons.
I've tried using this code in function2:
const char* str[1024] = { function1() };
but I had no success whatsoever. If I try
printf("%s\n", str[1]);
in function2, it just prints (null).
I've also tried using malloc, but I had no success.
//main function {it does its thing, it wouldn't interest us}
//function1 {it creates a const char* var[1024];)
//function2 {here I want to use the const char* var[1024]; from function1}
Observation: In function1, the const char* prints just fine what it needs to print.
I wish I could find a way to solve this. Thank you for your patience!
Later edit for code:
const char* function1()
{
lang = fopen("lang.csv", "r");
int i = 0;
char line[1024];
const char* word[1024];
char num[] = { 1 , 2 };
while (fgets(line, 1024, lang))
{
char* tmp = _strdup(line);
printf("Field 1 would be %s\n", getfield(tmp, num[0])); // NOTE strtok clobbers tmp
word[i] = getfield(tmp, num[0]);
i++;
free(tmp);
}
printf("%s\n", word[1]); //prints successfully
fclose(lang);
return NULL;
}
int function2() {
const char* word[1024] = { function1() };
printf("%s\n", word[1]); // failure, prints (null)
}