如何通过意外修改字符串? [英] How can a string by accidently modified?

查看:53
本文介绍了如何通过意外修改字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下代码没有用处:


#include< string.h>

#include< stdio.h>

#include< string.h>


int manip(char * str){


size_t len = strlen(str)-1;

if(len> = 3){

str [0] =''A'';

str [1] ='''B'';

printf("字符串的长度是:%d \ n",len);

}

else {

返回-1;

}

}


int main(int argc,char ** argv){


if(argc!= 2){

fprintf(stderr,不够arguements\\\
");

退出(1);

}


manip(argv [1]);

printf("修改后的值是:%s \ n",argv [1]);


argv [1] = NULL;

printf("新的修改值是:%s \ n",argv [1]);


返回0;

}


我真的不喜欢知道如何用任何优雅的方式来表达这一点。请用这个来承担

。怎么可能意外修改

argv [1]中的字符串?我可以看到类似malloc()返回NULL的东西,然后

可能就像把这个值传递给manip(),但除此之外,

真的看到了这个。


提前致谢

Chad

Given the following code that achieves no useful purpose:

#include <string.h>
#include <stdio.h>
#include <string.h>

int manip(char *str) {

size_t len = strlen(str)-1;
if(len >= 3) {
str[0] = ''A'';
str[1] = ''B'';
printf("The length of the string is: %d\n", len);
}
else {
return -1;
}
}

int main(int argc, char **argv){

if(argc !=2){
fprintf(stderr,"Not enough arguements\n");
exit(1);
}

manip(argv[1]);
printf("The modified value is: %s\n",argv[1]);

argv[1] = NULL;
printf("The new modified value is: %s\n",argv[1]);

return 0;
}

I really don''t know how to word this in any graceful way. Please bear
with this. How is it possible to accidently modify the string in
argv[1]? I can maybe see something like malloc() returning NULL, then
maybe like having this value be passed to manip(), but other than that,
really see this.

Thanks in advance
Chad

推荐答案

In文章< 11 ********************** @ g49g2000cwa.googlegroups .com>,

Chad< cd **** *@gmail.com>写道:
In article <11**********************@g49g2000cwa.googlegroups .com>,
Chad <cd*****@gmail.com> wrote:
argv [1] = NULL;
printf("新的修改值是:%s \ n",argv [1]);
argv[1] = NULL;
printf("The new modified value is: %s\n",argv[1]);




为%s格式传递NULL指针有不确定的结果。

-

所有都是虚荣。 - 传道书



Passing a NULL pointer for a %s format has undefined results.
--
All is vanity. -- Ecclesiastes




Walter Roberson写道:

Walter Roberson wrote:
文章< 11 ******* ***************@g49g2000cwa.googlegroups .com> ;,
Chad< cd ***** @ gmail.com>写道:
In article <11**********************@g49g2000cwa.googlegroups .com>,
Chad <cd*****@gmail.com> wrote:
argv [1] = NULL;
printf("新的修改值是:%s \ n",argv [1]);
argv[1] = NULL;
printf("The new modified value is: %s\n",argv[1]);



传递%s格式的NULL指针有不确定的结果。
-
一切都是虚荣。 - Ecclesiastes



Passing a NULL pointer for a %s format has undefined results.
--
All is vanity. -- Ecclesiastes




但仍然没有回答这个问题。



But still didn''t answer the question.




" Chad" < CD ***** @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

"Chad" <cd*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
鉴于以下代码没有用处:
#include< string.h>
#include< stdio.h>
#include< string.h>

int manip(char * str){

size_t len = strlen(str)-1;
if(len> = 3) {
str [0] =''A'';
str [1] =''B'';
printf("字符串的长度为:%d \ n,len);
}
其他{
返回-1;
}
}
int main(int argc,char ** argv){

if(argc!= 2){
fprintf(stderr,Not enough arguements\\\
);
exit(1); <操作(argv [1]);
printf(修改后的值为:%s \ n,argv [1]);

argv [1] = NULL;
printf(新修改的值是:%s \ n,argv [1]);

返回0;
}

我真的不知道如何以任何优雅的方式说出来。请用这个来承担
。如何在
argv [1]中意外修改字符串?我可以看到类似malloc()返回NULL的东西,然后
可能就像把这个值传递给manip(),但除此之外,
真的看到了这个。


不确定你的问题是什么?你期望这段代码做什么?


这有什么用......?

argv [1] = NULL;
printf("新的修改值是:%s \ n",argv [1]);
Given the following code that achieves no useful purpose:

#include <string.h>
#include <stdio.h>
#include <string.h>

int manip(char *str) {

size_t len = strlen(str)-1;
if(len >= 3) {
str[0] = ''A'';
str[1] = ''B'';
printf("The length of the string is: %d\n", len);
}
else {
return -1;
}
}

int main(int argc, char **argv){

if(argc !=2){
fprintf(stderr,"Not enough arguements\n");
exit(1);
}

manip(argv[1]);
printf("The modified value is: %s\n",argv[1]);

argv[1] = NULL;
printf("The new modified value is: %s\n",argv[1]);

return 0;
}

I really don''t know how to word this in any graceful way. Please bear
with this. How is it possible to accidently modify the string in
argv[1]? I can maybe see something like malloc() returning NULL, then
maybe like having this value be passed to manip(), but other than that,
really see this.
Not sure what your question is? What do you expect this code to do?

What''s the use of this ...?
argv[1] = NULL;
printf("The new modified value is: %s\n",argv[1]);




操作不会[明确]返回一个值,如果len> = 3.但是,因为你没有
使用函数'的返回值,我猜这就是'

让你感到惊讶吗?



manip doesn''t return [explicitly] a value if len >= 3. But, as you don''t
use the function''s returned value, I''m guessing that this is what''s
surprising you?


这篇关于如何通过意外修改字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆