I am new to this site, so I greatly apologize if I do anything wrong with this first post. The way I've written my code (including codes made for reusability), I must use char* arrays. I am converting the passed in char* into all lower-case letters. The problem I am facing deals with my freeing of newString2. Because i'm assigning it to my newString, freeing it would definitely lose the information I assigned to newString, thus losing the data assigned to convertedKey. I am in a hole as to finding out how to successfully free allocated memory, but also return the data of the converted string through my function argument. Is that possible? The following is my current function for converting to lower-case. I am also very new to strdup, so I'm sure I'm not giving it any justice.
void convertKeyCasing (ListElementPtr key, ListElement *convertedKey)
{
ListElementPtr newString = (ListElementPtr) malloc (sizeof (ListElement));
int i = 0;
char ch;
char *string = key->data.key.key;
char *newString1 = strdup (string);
while (isalpha (key->data.key.key[i]) != false)
{
ch = tolower(key->data.key.key[i]);
newString1[i] = ch;
++i;
}
char* newString2 = strdup (newString1);
newString->data.key.key = newString2;
*convertedKey = *newString;
key = convertedKey;
free(newString2);
free (newString1);
free (newString);
}
My structs in list include:
typedef struct KEY_DATATYPE
{
char* key;
int length;
}KEY_DATATYPE;
typedef struct DATATYPE
{
Q_DATATYPE data;
KEY_DATATYPE key;
// maintained so that it still works with list
union
{
char charValue;
unsigned int intValue;
float floatValue;
};
unsigned short whichOne;
} DATATYPE;
typedef struct ListElement* ListElementPtr;
typedef struct ListElement
{
DATATYPE data;
ListElementPtr psNext;
ListElementPtr psPrev;
} ListElement;
And in my driver, I'm creating a ListElementPtr and assigning it a key value, for example,
ListElementPtr listPtr;
listPtr->data.key.key = "Hello";
Thank you again for the help!