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.