Link to home
Create AccountLog in
Avatar of Chris120590
Chris120590

asked on

Concatenating to temporary string and returning address of the new string pointer

I am trying to figure out a way to make this function work.  This is what I have come up with an continue to hit a road block.

   char* createString(const int symbolsArray[], int numSymbols)

    int i;
    char *tempStr;

    tempStr = (char*)malloc(sizeof(MAX_LENGTH));

    for (i = 0; i < numSymbols; i++)
    {

    if (getSymbol(symbolsArray[i]) == NULL)
        {

            return NULL;

        }
        else
        {

            strcat(tempStr, getSymbol(symbolsArray[i]));


        }


    }

    return strdup(tempStr);

}

Open in new window



I am attempting to allocate memory for a temporary string.  The string will be concatenated with a returned string from another function already developed.  This function takes in an array of symbols that I want to iterate through by invoking the getSymbol function, which will return NULL or the address of the symbol.  If the function getSymbol returns NULL then the function will return NULL and free the memory allocated for tempStr.  If it returns the address of the symbol, I want it to concatenate the symbol string to the tempStr.  This will iterate until all symbols passed to the function have been tested.  Once tempStr contains the newly created string of symbols, the function should return the tempStr copy using strdup and the allocation of memory for tempStr should be freed up.

For some reason I cannot get the address of the newly created tempStr to pass and I am unsure of how to free up the memory if I am returning the tempStr.
SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Chris120590
Chris120590

ASKER

Kent,

The MAX_LENGTH is set to 250, far more than any of the strings I am creating.

I originally had the free(tempStr) before return NULL, just took it out while working on it.  Thanks for picking that up.

If I generate another temporary variable, wouldn't that be allocating memory that would need to be freed as well?
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I understand here is my modified code.  This function is called in Test Function, the function is supposed to return the address of the concatenated string which then is tested if its a string, if not returns an error message, otherwise the function uses strcmp(for the passed string address, and an expected string.  If it equals 0, it returns a successful message.  However, this is still not working.  When I looped through what was passed from the function below, garbage showed up infront of the string.

    int i;
    char *tempStr= NULL;
    char *result = NULL;

    tempStr = malloc(MAX_LENGTH*sizeof(char));


    for (i = 0; i < numSymbols; i++)
    {

        if (getSymbol(symbolsArray[i]) == NULL || numSymbols <= 0)
        {

            free(tempStr);
            return NULL;

        }
        else
        {

            strcat(tempStr, getSymbol(symbolsArray[i]));

        }

    }

    result = strdup(tempStr);
    free(tempStr);

    return result;

}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
It turns out that a dash is being placed infront of the first character in the array.  For instance, when I iterate through I am seeing -Hello instead of Hello, so my compare is not coming out correctly.  Do you know why?
Ignore the last entry, your advice corrected that issue before I read it.  Thanks for the help!  Learned a lot.  I knew I was on the right track but you got me to the finish line.