Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Params -> local vars? |
| Date | 2012-07-30 16:00 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <5016E7E6.3090109@verizon.net> (permalink) |
| References | <jv6nmr$15k$1@speranza.aioe.org> |
On 07/30/2012 03:35 PM, Jon Howson wrote:
> Hello
>
> I was reading thru some (yellowing!) mimeographed introductory notes on C
> and I found code like the below.
>
> main(argc,argv)
> int argc;
> char *argv[];
> {
> char *malloc();
> char *strcpy();
> int puts();
> void free();
>
> char *s;
> if(argc>1 &&(s=malloc(8+strlen(argv[1])))){
> strcpy(s,"Hello, ");
> strcpy(s+7,argv[1]);
> puts(s);
> free(s);
> }else{
> puts("Program failed, unknown error");
> }
> }
>
> I'm trying to work out what's happening. At the start of the function, I
> guess the parameters are being converted into local variables, maybe a
> way to get pass-by-value in C?
No - that's a non-prototyped function definition. They're still
supported by the C standard, but they've been obsolete ever since the
introduction of function prototypes in C90. Function prototypes turn on
various mandatory diagnostics, but as far as the actual behavior of the
code, that declaration is equivalent to:
main(int argc, char *argv[])
Note that no return type is specified. This used to be permitted; the
function was implicitly declared as returning an 'int'. However,
"implicit int" has not been supported since C99.
> Also what is the advantage of declaring the string and memory functions
> locally to the function?
You are allowed to provide your own declaration of some standard library
functions, so long as it is compatible with the correct declaration.
However, it's always a bad idea, even when permitted. You're much better
off #including the appropriate standard headers. In this case, those are
strings.h, stdio.h, and stdlib.h.
The advantage is very small, or possibly even negative: it restricts the
scope of those declarations to the body of main(). This allows
definition of identifiers with the same name and a different meaning
outside of main(), but that would still only work if they did not have
external linkage.
One disadvantage is that a declarations may become incorrect, even if it
originally was correct. That is in fact the case in this code. At one
time, before the first standard, malloc() returned a char*. However,
since the first standard malloc() has returned a void*. However, since
char* and void* are required to have the same representation and
alignment requirements, they are intended to be equivalent for this kind
of use; it would take a pretty perverse (but still fully conforming)
implementation of C for that to be a real problem.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Params -> local vars? Jon Howson <nospam@nospam.com> - 2012-07-30 19:35 +0000
Re: Params -> local vars? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-30 15:54 -0400
Re: Params -> local vars? James Kuyper <jameskuyper@verizon.net> - 2012-07-30 16:00 -0400
Re: Params -> local vars? Kenneth Brody <kenbrody@spamcop.net> - 2012-07-30 16:01 -0400
Re: Params -> local vars? Stephen Sprunk <stephen@sprunk.org> - 2012-07-30 15:09 -0500
Re: Params -> local vars? Mark Bluemel <mark_bluemel@pobox.com> - 2012-07-31 08:34 +0100
Re: Params -> local vars? Kleuske <kleuske@nowhere.net> - 2012-07-31 19:29 +0000
Re: Params -> local vars? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-31 15:51 -0400
Re: Params -> local vars? Kenneth Brody <kenbrody@spamcop.net> - 2012-08-01 15:58 -0400
Re: Params -> local vars? Keith Thompson <kst-u@mib.org> - 2012-07-30 13:49 -0700
Re: Params -> local vars? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-07-30 21:56 -0600
Re: Params -> local vars? David T. Ashley <dashley@gmail.com> - 2012-07-31 08:42 -0400
Re: Params -> local vars? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 13:39 -0700
Re: Params -> local vars? Keith Thompson <kst-u@mib.org> - 2012-09-07 17:55 -0700
Re: Params -> local vars? Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-08 05:09 -0700
Re: Params -> local vars? Keith Thompson <kst-u@mib.org> - 2012-09-08 11:14 -0700
Re: Params -> local vars? Richard Damon <news.x.richarddamon@xoxy.net> - 2012-09-08 16:43 -0400
Re: Params -> local vars? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 14:13 -0800
csiph-web