-4

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • You leak memory, you miss to do `free`. Also `if (surname = strpbrk(input, " "))` - you meant to `==` ? – KamilCuk Nov 13 '19 at 01:12
  • also... `if (name == NULL | surname == NULL)` ==> `if (name == NULL || surname == NULL)`. One | is bitwise or, two ||s is logical or. – bruceg Nov 13 '19 at 01:38
  • @KamilCuk No I want to assign strpbrk() to surname if it exists, which is what the implementation does... And yes the code is incomplete, which is why the free() hasn't been implemented yet... –  Nov 17 '19 at 02:40
  • Why have you deleted all the text of the original and replaced it? None of the answers or comments make any sense anymore – Ed Heal Nov 17 '19 at 15:13
  • 1
    please do not vandalize your post; I've rolled back the edits. – Martijn Pieters Nov 17 '19 at 16:31
  • `if ((length > 0) && (input[length - 1] == '\n')) input[length - 1] = '\0';` is better written as `input[ strcspn( input, "\n" ) ] = '\0';` See https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input – Andrew Henle Nov 17 '19 at 16:36

1 Answers1

1

I think you want strcpy(char* dest, char* src). Dereferencing a char* will simply yield you the first char in the string.

#include <stdio.h>

int main()
{
    char* test = "hello world";
    char* test2 = malloc(11*sizeof(char));
    strcpy(test2, test);

    printf("%s\n", test);
    printf("%s\n", test2);
    test2[5] = '_';
    printf("%s\n", test);
    printf("%s\n", test2);
    printf("%c\n", *test);

    free(test2);
}

spits out

hello world                                                                                                                                                                        
hello world                                                                                                                                                                        
hello world                                                                                                                                                                        
hello_world                                                                                                                                                                        
h
Drake Main
  • 540
  • 1
  • 8
  • 13