On the problem of string modification

topic description

modify a character in the string to be"0"

sources of topics and their own ideas

I found this problem when I was reading a project, and then after writing the relevant code, I found that there was still a problem, but I didn"t understand it.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

//code 1

-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>


char *
duo_split_at(char *s, char delimiter, unsigned int position)
{
    unsigned int count = 0;
    char *iter = NULL;
    char *result = s;

    for (iter = s; *iter; iterPP) {
        if (*iter == delimiter) {
            if (count < position) {
                result = iter + 1;
                countPP;
            }
            *iter = "\0";
        }
        if(*iter !="\0"){
            printf("%c\n",*iter);
        }
    }

    if (count < position) {
        return NULL;
    }

    return result;
}

int main(){
    const char delimiter = "/";
    const unsigned int delimited_position = 5;
    char *user;
   // char *pw_geco ;
    char *pw_geco = "code1/code2/code3//textField/usergecosparsed";
    user = duo_split_at(pw_geco, delimiter, delimited_position);

    printf("%s\n%s\n",user,pw_geco);

    return 0;
}

Please enter the code


//code 2

-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>


char *
duo_split_at(char *s, char delimiter, unsigned int position)
{
    unsigned int count = 0;
    char *iter = NULL;
    char *result = s;

    for (iter = s; *iter; iterPP) {
        if (*iter == delimiter) {
            if (count < position) {
                result = iter + 1;
                countPP;
            }
            *iter = "\0";
        }
        if(*iter !="\0"){
            printf("%c\n",*iter);
        }
    }

    if (count < position) {
        return NULL;
    }

    return result;
}

int main(){
    const char delimiter = "/";
    const unsigned int delimited_position = 5;
    char *user;
   // char *pw_geco ;
    char pw_geco[] = "code1/code2/code3//textField/usergecosparsed";
    user = duo_split_at(pw_geco, delimiter, delimited_position);

    printf("%s\n%s\n",user,pw_geco);

    return 0;
}
//code 3

-sharpinclude<stdio.h>
-sharpinclude<stdlib.h>


char *
duo_split_at(char *s, char delimiter, unsigned int position)
{
    unsigned int count = 0;
    char *iter = NULL;
    char *result = s;

    for (iter = s; *iter; iterPP)
    {
        if (*iter == delimiter)
        {
            if (count < position)
            {
                result = iter + 1;
                countPP;
            }
            *iter = "\0";
        }
        if(*iter !="\0")
        {
            printf("%c\n",*iter);
        }
    }

    if (count < position)
    {
        return NULL;
    }

    return result;
}

int
main ()
{
    char* user = "daijwei";
    struct passwd *pw;
    if((pw = getpwnam(user)) == NULL)
    {
        printf("error");
        return -1;
    }
    const char delimiter = "/";
    const unsigned int delimited_position = 5;
    user = duo_split_at (pw->pw_gecos, delimiter, delimited_position);

    printf ("%s\n%s\n", user, pw->pw_gecos);

    return 0;
}

what result do you expect? What is the error message actually seen?

assume that the user"s gecos format is "code1/code2/code3//textField/usergecosparsed"
. The effect of these three codes is to achieve the same function. If the usergecosparsed in linux user gecos is removed, an error will be reported in code1 and cannot be executed. After query, it is said that char pw_geco cannot be modified because it is a string defined by char . After the
is changed to code2, it can be executed smoothly.
but why does code3 execute smoothly? The pw_geocs defined in struct passwd is also defined by char*, so I am very confused to figure out why this is the case.

C
May.11,2021

at first I also felt that I could modify it successfully, but after trying your code, I also found the problem you said. Then after looking at the source code of getpwnam (), I think it can be explained in this way.
first of all, tell me why your first code is not successful:
char * pw_geco = "code1/code2/code3//textField/usergecosparsed";
what you define is to make the pointer variable pw_gecon point to the string constant "code1/code2/code3//textField/usergecosparsed", so then your code wants to change the value of this constant, so the system will not let you modify it. So if you change the code like this:
char * pw_geco=NULL;
char pw_geco= (char ) malloc (200); / / apply for memory for this pw_geco pointer variable and store the string.
sprintf (pw_geco, "% s", "code1/code2/code3//textField/usergecosparsed");
so that you change the pw_geco pointer variable through the change is not a constant string. It's a mutable string that pw_geco points to.


The problem with

is not that strings defined with char cannot be modified, but that string constants are assigned to char*.
ISO CPP prevents string literal constants from being converted to char*. For the literal constant "code1/code2/code3//textField/usergecosparsed", you can write

const char* str = "code1/code2/code3//textField/usergecosparsed";

const cannot be removed.
in terms of type, const T cannot be converted to T ; in terms of implementation, literal constants are placed in the constant segment of the program, and modification may trigger the protection mechanism.
there is nothing wrong with modifying the memory pointed to by the char* variable, so code3 can be executed smoothly.

Menu