0

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?

Hazim
  • 381
  • 1
  • 7
  • 24

2 Answers2

2

You make final point to the local array subject, and then return that pointer. That means you are in fact returning a pointer to the local subject array, an array that goes out of scope once the function returns, leaving you with a stray pointer and undefined behavior.


You need to allocate memory for the final string, and there are generally two ways of doing it:

  1. Use e.g. strdup in the function to allocate memory and return a pointer to that new memory.

  2. Pass in a pointer to a buffer of enough memory as an argument to the function, and copy the string to that buffer.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Okay - so I added `char* finalSubject` to the function call, and used `strcpy(finalSubject, subject)` and called the function as `getSubject(addresses, subject, 43);` - and now I'm getting a segmentation error? – Hazim Apr 06 '15 at 17:25
  • @Hazim That's because you need to allocate memory to `finalSubject` before copying to it. Added a couple of solutions. – Some programmer dude Apr 06 '15 at 17:30
1

you are returning a pointer to a stack variable, you cant do that. The stack is cleared once you return. Make a copy

 return strdup(final);

the call must free it when finished with it

pm100
  • 48,078
  • 23
  • 82
  • 145