库实现strncpy()修改字符串文字,合法吗?更多疑惑需要更多建议吗? [英] library implementation strncpy() modifies string literal, legal? more doubts need more suggestion?

查看:64
本文介绍了库实现strncpy()修改字符串文字,合法吗?更多疑惑需要更多建议吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。在下面的代码中,代码(第11行)是否合法?文档中是否有

通知告诉调用者参数s1应该接收数组变量,即键入char [],但不是char
*? p1和p2指向相同的东西,但它们必须被声明为

不同的类型?是自然吗?


char p1 [] =" hello123456";

char * p2 =" world";

strncpy(p1,p2,strlen(p2));


并且在

参数s1的声明中缺少const关键字(行2)指示s1接收一个参数

数组?

2.临时变量os1(第4行)仅作为返回使用一次

值(第12行)。除了声明(第4行)之外,在整个

函数中使用os1而不是s1更好。或者只是不介绍

临时os1,这也是个好主意吗?您更喜欢哪种风格?


/ *来自: http://cvs.opensolaris.org/source/xr...til/string.c*/ < br $>
char *

strncpy(char * s1,const char * s2,size_t n)/ * line 2 * /

{

char * os1 = s1; / *第4行* /

n ++;

while(--n!= 0&&(* s1 ++ = * s2 ++)!=' '\0'')

;

if(n!= 0)

while(--n!= 0)

* s1 ++ =''\'''; / *第11行* /

return(os1); / *第12行* /

}

-

lovecreatesbeauty

1. In the following code, is the code (line 11) legal? Is there a
notice in the document to tell callers that the parameter s1 should
receive an array variable, i.e. type char[], but not a variable of char
*? p1 and p2 point to the same things but they must be declared as
different types? Is it nature?

char p1[] = "hello123456";
char *p2 = "world";
strncpy(p1, p2, strlen(p2));

And is the lack of the const keyword in the declaration of the
parameter s1 (line 2) an indication that s1 receives an argument of
array?
2. The temporary variable os1 (line 4) is used only once as the return
value (line 12). Is it better to use os1 instead of s1 throughout the
function except the declaration (line 4). Or just don''t introduce the
temporary os1, is it also a good idea? Which style do you prefer?

/*from:
http://cvs.opensolaris.org/source/xr...til/string.c*/
char *
strncpy(char *s1, const char *s2, size_t n) /*line 2*/
{
char *os1 = s1; /*line 4*/

n++;
while (--n != 0 && (*s1++ = *s2++) != ''\0'')
;
if (n != 0)
while (--n != 0)
*s1++ = ''\0''; /*line 11*/
return (os1); /*line 12*/
}
--
lovecreatesbeauty

推荐答案

lovecreatesbeauty写道:
lovecreatesbeauty wrote:
主题:库实现strncpy()修改字符串文字,合法吗?


实施任何它想要的东西是完全合法的,所以



因为它满足要求C标准的语义[假设

实现声称符合。]

更多疑问需要更多建议吗?
Subject: library implementation strncpy() modifies string literal, legal?
It''s perfectly legal for an implementation to do whatever it wants, so
long
as it forfills the required semantics of the C standard [assuming the
implementation claims conformance.]
more doubts need more suggestion?




如果之前没有向你推荐过,那么我会建议你现在给你:



阅读常见问题。

http://c-faq.com/


例如..

http://c-faq.com/decl/strlitinit.html


strncpy函数允许修改指向的字符串

第一个参数,因为它是_supposed_要执行的操作。


字符串文字有const []类型,但是不可修改(对于

歇斯底里

原因,俗话说...... )


< snip>


-

彼得



If it hasn''t been suggested to you before, then I''ll suggest it to you
now:

Read the FAQ.

http://c-faq.com/

e.g...

http://c-faq.com/decl/strlitinit.html

The strncpy function is allowed to modify the string pointed to by the
first argument because that is what it is _supposed_ to do.

String literals have const[] type, but are non-modifiable (for
hysterical
reasons, as the saying goes...)

<snip>

--
Peter


lovecreatesbeauty说:
lovecreatesbeauty said:
1.在下面的代码中,代码(第11行)是否合法?文档中是否有通知告诉调用者参数s1应该接收数组变量,即键入char [],但不是char的变量
*? p1和p2指向相同的东西,但它们必须被声明为不同的类型?是自然吗?

char p1 [] =" hello123456" ;;
char * p2 =" world";
strncpy(p1,p2,strlen(p2)) ;


合法但通常不明智。请注意,p1 []现在的内容为world123456,

不仅仅是world。

并且声明中缺少const关键字
参数s1(第2行)指示s1接收
数组的参数?


否。这意味着该函数需要能够从s1指示的地址开始写入n

个字符,所以你会更好地制作

确定它可以。

2.临时变量os1(第4行)仅用作返回值
(第12行)。除了声明(第4行)之外,在整个
函数中使用os1而不是s1更好。或者只是不介绍
临时os1,这也是个好主意吗?你更喜欢哪种风格?
1. In the following code, is the code (line 11) legal? Is there a
notice in the document to tell callers that the parameter s1 should
receive an array variable, i.e. type char[], but not a variable of char
*? p1 and p2 point to the same things but they must be declared as
different types? Is it nature?

char p1[] = "hello123456";
char *p2 = "world";
strncpy(p1, p2, strlen(p2));
Legal but often unwise. Note that p1[] now has the contents "world123456",
not just "world".
And is the lack of the const keyword in the declaration of the
parameter s1 (line 2) an indication that s1 receives an argument of
array?
No. It means that the function requires to be able to write to the n
characters starting at the address indicated by s1, so you''d better make
sure it can.
2. The temporary variable os1 (line 4) is used only once as the return
value (line 12). Is it better to use os1 instead of s1 throughout the
function except the declaration (line 4). Or just don''t introduce the
temporary os1, is it also a good idea? Which style do you prefer?




标准,正确或错误,需要strncpy返回指针

在第一个参数中收到的值。如果要实现其目标,它还需要从该点移动指针

。无论哪种方式,都必须制作该值的副本
。哪个指针用于沿着

数组移动,而用于存储原始值的指针并没有产生
盎司的差异。


如果您愿意,您可以获得一份可以随意涂鸦的复印件。你需要在它上面涂抹
涂鸦,但你还需要保留一份未经编写的副本。所以

你复印复印件。这是编程,副本是完美的。

你现在有两个副本 - 一个是最好的,一个是涂鸦。它是用于b $ b你用于哪种用途的问题?当然不是。他们是相同的

任何实际目的。


-

Richard Heathfield

" Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



The Standard, rightly or wrongly, requires strncpy to return the pointer
value it receives in the first parameter. It also needs to move a pointer
along from that point if it is to achieve its goal. So either way, a copy
of that value has to be made. Which pointer is used for moving along the
array and which is used for storing the original value doesn''t make an
ounce of difference.

You are given a photocopy which you can scribble on if you wish. You need to
scribble on it, but you also need to keep hold of an unscribbled copy. So
you photocopy the photocopy. This being programming, the copy is perfect.
You now have two copies - one for best, and one for scribbling on. Does it
matter which you use for which usage? Of course not. They are identical for
any practical purpose.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)




Peter Nilsson写道:

Peter Nilsson wrote:
lovecreatesbeauty写道:
lovecreatesbeauty wrote:
主题:库实现strncpy()修改字符串文字,合法吗?
执行它是完全合法的无论它想要什么,所以
长,因为它填补了C标准所要求的语义[假设
实现声称符合。]
Subject: library implementation strncpy() modifies string literal, legal?
It''s perfectly legal for an implementation to do whatever it wants, so
long
as it forfills the required semantics of the C standard [assuming the
implementation claims conformance.]




我不怀疑修改的行为,但怀疑它的实现或接口是什么。你可以为你提供专业知识,并且对他人更耐心。

字符串文字有const []类型,但是不可修改(对于
歇斯底里的
原因,俗话说...)



I''m not doubting the behavior of modification, but doubting on its
implementation or interface. You could provide you expertise and be
more patient to others.
String literals have const[] type, but are non-modifiable (for
hysterical
reasons, as the saying goes...)




你不觉得以下2行修改字符串文字吗?


char * s =" hysterical";

s [1] =''H'';

-

lovecreatesbeauty



Don''t you think the following 2 lines are modifying a string literal?

char *s = "hysterical";
s[1] = ''H'';
--
lovecreatesbeauty


这篇关于库实现strncpy()修改字符串文字,合法吗?更多疑惑需要更多建议吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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