简体   繁体   中英

How to dereference a string pointer

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM