You are not correctly null-turning off strings in key_word and value_word , and this error is apparently spreading across. This loop is a problem:
for(j=0;j<key_length;j++){ key_word[j] = *colon; colon++; }
It copies the key_length characters to key_word , but none of these copied characters is a null terminator. You can fix the problem by adding one extra byte to key_word :
char key_word[key_length + 1];
Then adding this after the for() loop:
key_word[key_length] = '\0';
There is also no need to create copies in <T29> and finalvalue (which are of the wrong type, in any case - thatβs why you ultimately need all these ugly casts). Therefore, in general, it would look like this:
char key_word[key_length + 1]; char value_word[value_length + 1]; for (j = 0; j < key_length; j++) { key_word[j] = *colon; colon++; } key_word[key_length] = '\0'; space++; for(k = 0; k < value_length; k++) { value_word[k] = *space; space++; } value_word[value_length] = '\0'; dictionary_add(d, key_word, value_word);
Indeed, you should simplify this function using the tools from string.h . For example, strstr() will allow you to search for ": " string that separates the key and value, and memcpy() makes the equivalent of these for() loops:
int dictionary_parse(dictionary_t *d, char *key_value) { char *colon; char *value; int key_length = -1; //Default key length to check for failure colon = strstr(key_value, ": "); if (colon != NULL) { key_length = colon - key_value; // Number of characters before the colon value = colon + 2; // Value is portion of the string after ": " } if (key_length < 1) { return -1; } char key_word[key_length + 1]; memcpy(key_word, key_value, key_length); key_word[key_length] = '\0'; dictionary_add(d, key_word, value); return 0; }