一个愚蠢的strcat问题 [英] a dumb strcat question
问题描述
如何编写一个包含输入字符串的字符串函数
in quotes" string" ??
适用于123操作但添加对它来说
clobbers123
main()
{
char * sfun(char *) ;
printf("%s",sfun(" 123"));
}
char * sfun (char * s)
{
static char * quote =" \"" ;;
char * outs;
outs = quote;
strcat(outs,s); // outs =" 123
strcat(出,引用); // outs = nada为什么?
返回(出局);
}
How can I write a string function that encloses the input string
in quotes "string" ??
below works for the "123 operation but adding " to it
clobbers the "123
main()
{
char *sfun(char *);
printf("%s",sfun("123"));
}
char *sfun(char *s)
{
static char *quote = "\"";
char *outs;
outs = quote;
strcat(outs,s); // outs = "123
strcat(outs,quote); // outs = nada why?
return (outs);
}
推荐答案
David Frank< da ******** @ hotmail.com>潦草地写了下面的内容:
David Frank <da********@hotmail.com> scribbled the following:
如何编写一个字符串函数,将输入字符串
括在引号string中。 ??
以下适用于123操作但添加操作对它来说,123
main()
{*> char * sfun(char *);
printf(" ;%s",sfun(" 123"));
}
char * sfun(char * s)
{
static char * quote =" \" ;" ;;
char * outs;
outs = quote;
strcat(outs,s); // outs =" 123
否。这会通过修改文字字符串导致未定义的行为。
strcat(outs,quote); // outs = nada为什么?
好吧,如果我们假设上面的strcat调用成功(并且你认为它做了什么),那么out == quote仍然是真的,
所以这个电话实际上是strcat(出局,出局)。扫描一个字符串
自己是一个非常有趣的尝试...
返回(出局);
}
How can I write a string function that encloses the input string
in quotes "string" ??
below works for the "123 operation but adding " to it
clobbers the "123
main()
{
char *sfun(char *);
printf("%s",sfun("123"));
} char *sfun(char *s)
{
static char *quote = "\"";
char *outs;
outs = quote;
strcat(outs,s); // outs = "123
No. This causes undefined behaviour by modifying a literal string.
strcat(outs,quote); // outs = nada why?
Well, if we assume that the above strcat call succeeded (and did
what you thought it did), then outs==quote will still be true,
so this call is effectively strcat(outs, outs). strcatting a string
onto itself is quite an interesting thing to try...
return (outs);
}
也许你可以试试这个吗?
char * sfun(char * s)
{
char * outs = malloc(strlen(s)+3);
if(outs){
sprintf(outs," \"%s \" ;",s);
}
退货;
}
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \\ \\
\ - http://www.helsinki。 fi / ~palaste ---------------------规则! -------- /
嘘!大师正在分解!
- Gary Larson
Perhaps you could try this?
char *sfun(char *s)
{
char *outs = malloc(strlen(s)+3);
if (outs) {
sprintf(outs, "\"%s\"", s);
}
return outs;
}
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson
在comp.lang.ci中读取:
in comp.lang.c i read:
如何编写一个字符串函数,将输入字符串
括在引号" string"中??
How can I write a string function that encloses the input string
in quotes "string" ??
怎么样?你可以学习c。因为这是不可能的,并且由于原因,即使我无法理解,因此我将为您提供这样的功能:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
/ *
`reput''输入字符串。现有的报价没有被转义。
返回新的存储空间,来电者负责处理。
* /
char * sfun(const char * const s)
{
char * new = malloc(strlen(s)+ 3);
if(0!= new)sprintf( new," \"%s \"",s);
返回新的;
}
示例用法:
#include< stdio.h>
#include< stdlib.h>
char * sfun(const char * const s);
int main(void)
{
char * p =" some string", * q = sfun(p);
fputs(" original:",stdout); puts(p);
fputs(" processed:",stdout); puts(q);
免费(q);
返回0;
}
-
a签名
how? you could learn c. since this isn''t likely, and for reasons even i
cannot understand, i will therefore provide you with such a function:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
`quote'' the input string. existing quoting is not escaped.
returns fresh storage, caller is responsible for disposal.
*/
char *sfun(const char *const s)
{
char *new = malloc(strlen(s) + 3);
if (0 != new) sprintf(new, "\"%s\"", s);
return new;
}
example usage:
#include <stdio.h>
#include <stdlib.h>
char *sfun(const char * const s);
int main(void)
{
char *p = "some string", *q = sfun(p);
fputs("original : ", stdout); puts(p);
fputs("processed: ", stdout); puts(q);
free(q);
return 0;
}
--
a signature
" Joona I Palaste" < PA ***** @ cc.helsinki.fi>在留言中写道
news:bq ********** @ oravannahka.helsinki.fi ...
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bq**********@oravannahka.helsinki.fi...
David Frank< da **** ****@hotmail.com>潦草地写了下面的内容:
David Frank <da********@hotmail.com> scribbled the following:
如何编写一个字符串函数,将输入字符串
括在引号string中。 ??
以下适用于123操作但添加操作to it it
clobbers the123
How can I write a string function that encloses the input string
in quotes "string" ??
below works for the "123 operation but adding " to it
clobbers the "123
main()
{* char * sfun(char *);
printf(" ;%s",sfun(" 123"));
}
main()
{
char *sfun(char *);
printf("%s",sfun("123"));
}
char * sfun(char * s)
{
static char * quote =" \"" ;;
char * outs;
outs = quote;
strcat(outs,s); // outs =" 123
char *sfun(char *s)
{
static char *quote = "\"";
char *outs;
outs = quote;
strcat(outs,s); // outs = "123
否。这会通过修改文字字符串导致未定义的行为。
No. This causes undefined behaviour by modifying a literal string.
strcat(outs,quote); // outs = nada为什么?
strcat(outs,quote); // outs = nada why?
好吧,如果我们假设上面的strcat调用成功(并且确实是你认为它做了什么),那么out == quote will仍然是真的,
所以这个电话实际上是strcat(出局,出局)。尝试将一个字符串拉到自身上是一件非常有趣的事情......
Well, if we assume that the above strcat call succeeded (and did
what you thought it did), then outs==quote will still be true,
so this call is effectively strcat(outs, outs). strcatting a string
onto itself is quite an interesting thing to try...
return(outs);
}
return (outs);
}
<或许你可以试试这个吗?
char * sfun(char * s)
{
char * outs = malloc(strlen(s)+3);
if(outs){
sprintf(outs," \"%s \"",s);
}
退货;
}
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------
Perhaps you could try this?
char *sfun(char *s)
{
char *outs = malloc(strlen(s)+3);
if (outs) {
sprintf(outs, "\"%s\"", s);
}
return outs;
}
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) -------------
芬兰-------- \ \ - http://www.helsinki.fi/~palaste ---------------------
规则! -------- /嘘!大师正在分解!
- Gary Larson
Finland --------\ \-- http://www.helsinki.fi/~palaste ---------------------
rules! --------/ "Shh! The maestro is decomposing!"
- Gary Larson
上面的malloc语句会收到警告(如下),但例程
输出123根据要求,以下警告在我所有的C编程尝试中都是不变的,并且是主要原因
我仍然是Fortran程序员。
(有点让我< smile>看到它咬了经验丰富的C程序员)
感谢!!
test.c(8):警告C4047:''初始化'':''char *''的级别不同
的间接来自''int''
Microsoft(R)Incremental Linker Version 6.00.8447
版权所有(C)Microsoft Corp 1992-1998。保留所有权利。
/out:test.exe
test.obj
The malloc statement above gets a warning (below), but the routine
outputs "123" as requested, the following warning is a constant
bane in ALL my attempts at C programming and is a chief reason
I remain a Fortran programmer.
(kinda makes me <smile> to see it bite experienced C programmers)
THANKS!!
test.c(8) : warning C4047: ''initializing'' : ''char *'' differs in levels
of indirection from ''int ''
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:test.exe
test.obj
这篇关于一个愚蠢的strcat问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!