472,958 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

fgets prototype doesn't include 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?)

TIA, Jaime :-)
Aug 15 '07 #1
10 2298
jaime wrote:
According to the fgets wikipedia page, its prototype is:

char* fgets(char *string, int length, FILE * stream)
Yes.
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?
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

Aug 15 '07 #2
jaime said:
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?)
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
Aug 15 '07 #3
jxh
On Aug 15, 4:41 am, Richard Heathfield <r...@see.sig.invalidwrote:
[snip]
>
#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;
int _c;
while(_n-- 1 && (_c = getc(_f)) != EOF)
{
*_b++ = _c;
/* _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 != EOF)
{
*_b = '\0';
}
else
{
_r = NULL;
}
return _r;

}
-- James

Aug 15 '07 #4
jxh said:
On Aug 15, 4:41 am, Richard Heathfield <r...@see.sig.invalidwrote:
[snip]
>>
#include <stdio.h>

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

Good thing for the warning.
Nevertheless, good catches.

<snip>

--
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
Aug 15 '07 #5
jaime wrote:
>
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?)
What you are missing is that consts
are always omitted on the parameters.

There are no const qualified parameters
in any standard library function,
for reasons that Richard Heathfield has
already explained elsewhere in this thread.

--
pete
Aug 15 '07 #6
pete wrote:
jaime wrote:
>>
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?)

What you are missing is that consts are always omitted on the
parameters. There are no const qualified parameters in any
standard library function, for reasons that Richard Heathfield
has already explained elsewhere in this thread.
You are mistaken. Here is an example from N869:

7.21.2.3 The strcpy function

Synopsis
[#1]
#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 16 '07 #7
CBFalconer said:
pete wrote:
<snip>
>>
What you are missing is that consts are always omitted on the
parameters. There are no const qualified parameters in any
standard library function, for reasons that Richard Heathfield
has already explained elsewhere in this thread.

You are mistaken. Here is an example from N869:

7.21.2.3 The strcpy function

Synopsis
[#1]
#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);
You are mistaken. The constness here applies to the thing pointed at,
not the pointer itself (i.e. the parameter, which is what pete was
talking about).

--
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
Aug 16 '07 #8
On Aug 15, 10:15 pm, CBFalconer <cbfalco...@yahoo.comwrote:
pete wrote:
What you are missing is that consts are always omitted on the
parameters. There are no const qualified parameters in any
standard library function, for reasons that Richard Heathfield
has already explained elsewhere in this thread.

You are mistaken. Here is an example from N869:

7.21.2.3 The strcpy function

Synopsis
[#1]
#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);
While that prototype does use the "const" keyword, the parameter
itself is not constant. In this instance, it means that the characters
pointed to by "s2" will not be modified. The others were discussing
definitions such as:

char *strcpy(char * const restrict s1, const char * const restrict
s2);

in which the values of the parameters themselves would not be able to
be changed in the implementation of the function, which is kind of a
ridiculous restriction, seeing as it does not affect the calling code.

Aug 16 '07 #9
Richard Heathfield wrote:
CBFalconer said:
>pete wrote:
<snip>
>>>
What you are missing is that consts are always omitted on the
parameters. There are no const qualified parameters in any
standard library function, for reasons that Richard Heathfield
has already explained elsewhere in this thread.

You are mistaken. Here is an example from N869:

7.21.2.3 The strcpy function

Synopsis
[#1]
#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);

You are mistaken. The constness here applies to the thing pointed
at, not the pointer itself (i.e. the parameter, which is what
pete was talking about).
I don't even consider that, since parameters are by value, and any
const attribute can't possibly affect the caller in any way. To my
mind your restriction is totally useless.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 16 '07 #10
CBFalconer <cb********@yahoo.comwrites:
Richard Heathfield wrote:
>CBFalconer said:
>>pete wrote:
<snip>
>>>>
What you are missing is that consts are always omitted on the
parameters. There are no const qualified parameters in any
standard library function, for reasons that Richard Heathfield
has already explained elsewhere in this thread.

You are mistaken. Here is an example from N869:

7.21.2.3 The strcpy function

Synopsis
[#1]
#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);

You are mistaken. The constness here applies to the thing pointed
at, not the pointer itself (i.e. the parameter, which is what
pete was talking about).

I don't even consider that, since parameters are by value, and any
const attribute can't possibly affect the caller in any way.
Yes, that's exactly the point. The OP was asking why fgets is declared as
char* fgets(char *string, int length, FILE * stream);
rather than
char* fgets(char * const string, int length, FILE * stream);

Both pete and Richard were answering that question. I'll grant you
that pete's statement that "[t]here are no const qualified parameters
in any standard library function" was worded imprecisely (it happens).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 16 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
7
by: Emerson | last post by:
Hi all: my problem is in the comments of the code . would u please give me some clue what 's the problem? what u in advance. /*******************************/ #include "stdio.h" #include...
13
by: Roy Hills | last post by:
I've seen two different function prototype formats used for ANSI C, and I'm unsure as to which is the correct or preferred one. 1st Format (this is what I use) type function(type, type, type);...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
9
by: Justme | last post by:
Novice programmer needs help with using fgets to read and ignore the first two lines of a file. I've gone thru the previous posting regarding fgets, but none of them seems to help my situation. I...
11
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
285
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
3
by: FireHead | last post by:
Hello comp.lang.c Couple of years ago I created a topic as to how I can use the function fgets so that I can read a line of text from a source of data. After a long time I have finally got all...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.