I'm having issues getting the desired behaviour out of my code. I'm new to the C language (although I usually program in C++ so I'm sort of familiar), and I'm unsure where to go with my issue. In the below code, when attempting to populate the 2 strings with the contents of the "input" string passed to the function, the result is that the memory locations are passed to the 2 strings, meaning when I perform modifications on the individual strings, the original data is edited... I want to have it such that the initial data is copied to the new memory locations allocated to the new strings.
bool interpretInput(char *input)
{
char *name = malloc(MAX_NAME_SIZE / 2);
char *surname = malloc(MAX_NAME_SIZE / 2);
if (name == NULL | surname == NULL)
{
return 1;
}
int length = (int) strlen(input);
if ((length > 0) && (input[length - 1] == '\n')) input[length - 1] = '\0';
if (surname = strpbrk(input, " "))
{
int sLength = (int) strlen(surname);
for (int i = 0; i < sLength; i++)
{
surname[i] = surname[i + 1];
}
length = (int) strlen(input);
for (int i = 0; i <= length - sLength; i++)
{
name[i] = input[i];
}
length = (int) strlen(name);
sLength = (int) strlen(surname);
printf("Name: %s length: %d \n", name, length);
printf("Surname: %s length: %d \n", surname, sLength);
}
else
{
printf("Name length: %d", length);
}
return 0;
}
EDIT: I, effectively, want to populate one array with the values at each increment of another array, without using strcpy.