I have a dynamically allocated array of pointers, each of which points to a line of text in a file. I need to extract the fourth column of the file (the values in the file are separated by commas), so that I can be able to sort the file by that column later on. I've been able to extract the fourth column text, however I am unable to return it to use in my main function, and when I return it, it's blank, what could be the problem?
getStart finds the comma right before the needed column
int getStart(char** addresses, int index)
{
int i = 0;
int count = 0;
for(i=0;i < strlen(addresses[index]); i++)
{
if(addresses[index][i] == ',')
count += 1;
if(count == 3)
return i+1;
}
}
getStop finds the comma right before the end of the needed column
int getStop(char** addresses, int index)
{
int i = 0;
int count = 0;
for(i=0;i < strlen(addresses[index]); i++)
{
if(addresses[index][i] == ',')
count += 1;
if(count == 4)
return i;
}
}
getSubject takes all the characters in between the start and stop, and puts them into an array (which is what I need to return to main)
char* getSubject(char** addresses, int index)
{
int i = 0;
int start;
int stop;
char* final;
char subject[10];
start = getStart(addresses, index);
stop = getStop(addresses, index);
for(start; start<stop; start++)
{
subject[i] = addresses[index][start];
i++;
}
final = subject;
printf("%s %s\n", final, subject);
return final;
}
The way that I know the function work is that
printf("%s %s\n", final, subject);
prints the proper values, however when I call it in main as follows:
char* test;
test = getSubject(addresses, 43);
printf("%s\n", test);
test prints a blank value. Why is this?