fgets原型不包括const? [英] fgets prototype doesn't include const?

查看:71
本文介绍了fgets原型不包括const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


根据fgets维基百科页面,它的原型是:


char * fgets(char * string,int长度,文件*流)


鉴于fgets从未分配给第一个参数(char

指针),原型也可以定义为:


char * fgets(char * const string,int length,FILE * stream)


我在这里遗漏了什么? (或者它真的是const经常

省略吗?)


TIA,Jaime :-)

Hi all.

According to the fgets wikipedia page, its prototype is:

char* fgets(char *string, int length, FILE * stream)

Given that fgets never assigns to the first parameter (the char
pointer), could the prototype also have been defined as:

char* fgets(char * const string, int length, FILE * stream)

Am I missing something here? (or is it really that "const"s are frequently
omitted?)

TIA, Jaime :-)

推荐答案

jaime写道:
jaime wrote:

根据fgets维基百科页面,它的原型是:

char * fgets(char * string,int length,FILE * stream)
According to the fgets wikipedia page, its prototype is:

char* fgets(char *string, int length, FILE * stream)



是。

Yes.


鉴于fgets从不指定第一个参数(char

指针),原型也可以定义为:


char * fgets(char * const string,int length,FILE * stream)
Given that fgets never assigns to the first parameter (the char
pointer), could the prototype also have been defined as:

char* fgets(char * const string, int length, FILE * stream)



没有任何区别。

Doesn''t make any difference.


我错过了什么吗?
Am I missing something here?



在原型中放置`const`是无关紧要的。它在函数/ definition /中产生了一个

的差异,它使得

指针无法分配,但这对
不感兴趣。
原型声明,因为无论如何都无法分配参数




[1]除了初始化之外当然是电话。


-

Hewlett-Packard Limited注册办事处:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN

Putting that `const` in the prototype is irrelevant. It makes a
difference in the function /definition/, where it makes the
pointer un-assignableto, but that''s not of interest in the
prototype declaration, since one can''t assign to the parameter
anyway [1].

[1] Apart from the initialisation on the call, of course.

--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


jaime说:
jaime said:

大家好。


根据fgets维基百科页面,它的原型是:


char * fgets(char * string,int length,FILE * stream)


鉴于fgets从不指定第一个参数(char

指针),原型也可以定义为:


char * fgets(char * const string,int leng th,FILE * stream)


我错过了什么吗? (或者它真的是const是

经常被省略吗?)
Hi all.

According to the fgets wikipedia page, its prototype is:

char* fgets(char *string, int length, FILE * stream)

Given that fgets never assigns to the first parameter (the char
pointer), could the prototype also have been defined as:

char* fgets(char * const string, int length, FILE * stream)

Am I missing something here? (or is it really that "const"s are
frequently omitted?)



你确实遗漏了什么。不保证fgets永远不会分配给第一个参数,如果确实如此,那就没关系了,因为

C是传值,所以输入

参数const没有特别的价值。这样做会对fgets的实现施加一个完全不必要的限制。


例如,它可能想要像这样工作:


#include< stdio.h>


/ *小心 - 这只是一个草图,而不是经过测试的实现* /

char * fgets(char * _b,int _n,FILE * _f)

{

char * _r = _b;

while (_n-- 1&&(* _b = getc(_f))!= EOF)

{

++ _ b; / * _b在这里进行了修改 - 这是一个本地更改,只影响了fgets所拥有的对象
- 它对于该值没有任何

的影响调用期间在参数中使用的对象

表达式。 * /

}

if(* _ b!= EOF)

{

* _b =''\\ \\ 0'';

}

else

{

_r = NULL;

}

返回_r;

}


如果_b是const限定的,则此代码将无法编译。 />

-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

You are indeed missing something. It is not guaranteed that fgets never
assigns to the first parameter, and it wouldn''t matter if it did, since
C is pass-by-value, so there''s no particular value in making the input
parameter const. To do so would place an entirely unnecessary
restriction on the implementation of fgets.

For example, it might want to work something like this:

#include <stdio.h>

/* beware - this is just a sketch, not a tested implementation */
char *fgets(char *_b, int _n, FILE *_f)
{
char *_r = _b;
while(_n-- 1 && (*_b = getc(_f)) != EOF)
{
++_b; /* _b is modified here - this is a local change, affecting
only the object owned by fgets - it doesn''t have any
effect on the value of the object used in the argument
expression during the call. */
}
if(*_b != EOF)
{
*_b = ''\0'';
}
else
{
_r = NULL;
}
return _r;
}

If _b were const-qualified, this code would not compile.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


8月15日凌晨4:41,Richard Heathfield< r ... @ see.sig.invalidwrote:

[snip]
On Aug 15, 4:41 am, Richard Heathfield <r...@see.sig.invalidwrote:
[snip]

>

#include< stdio.h>


/ *要注意 - 这只是一个草图,而不是经过测试的实现* /
>
#include <stdio.h>

/* beware - this is just a sketch, not a tested implementation */



警告的好处。需要一些修正。

Good thing for the warning. A couple of fixups are needed.


char * fgets(char * _b,int _n,FILE * _f)

{

char * _r = _b;
char *fgets(char *_b, int _n, FILE *_f)
{
char *_r = _b;



int _c;

while(_n-- 1&&(_ c = getc(_f))!= EOF)

int _c;
while(_n-- 1 && (_c = getc(_f)) != EOF)


{
{



* _b ++ = _c;

*_b++ = _c;


/ * _b在这里被修改 - 这是一个本地更改,只影响了fgets所拥有的对象
- 它不是对调用期间参数

表达式中使用的对象的值有任何

的影响。 * /
/* _b is modified here - this is a local change, affecting
only the object owned by fgets - it doesn''t have any
effect on the value of the object used in the argument
expression during the call. */



if(_c ==''\ n'')

{

break;

}

if (_c == ''\n'')
{
break;
}


}
}



if(_c!= EOF)

if(_c != EOF)


{

* _b =''\ 0'';

}

else

{

_r = NULL;

}

return _r;


}
{
*_b = ''\0'';
}
else
{
_r = NULL;
}
return _r;

}



- James

-- James


这篇关于fgets原型不包括const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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