Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23998

Re: Params -> local vars?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Params -> local vars?
Date 2012-07-30 13:49 -0700
Organization None to speak of
Message-ID <lnmx2h2cvx.fsf@nuthaus.mib.org> (permalink)
References <jv6nmr$15k$1@speranza.aioe.org>

Show all headers | View raw


Jon Howson <nospam@nospam.com> writes:
> 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[];

This is an old-style function definition.  It was the only form
available before the 1989 ANSI C standard introduced prototypes
(borrowed from C++).  It's still legal -- except for the omission of the
return type of main, which has been invalid since C99.

The modern equivalent is:

    int main(int argc, char *argv[]) { ... }

which has the advantage that it makes the parameter types visible to
callers (which is typically less relevant for main than for other
functions).

> {
>   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?

As I wrote above, it's an old-style definition.  (Parameters are really
local variables anyway; they differ from other local variables in how
they're declared and in the way they're initialized with the values of
the arguments given in the call.)

> Also what is the advantage of declaring the string and memory functions 
> locally to the function?

None.  The right way to get those declarations is:

    #include <stdlib.h> /* malloc, free */
    #include <string.h> /* strcpy */
    #include <stdio.h>  /* puts */

which also has the advantage of providing prototypes, so the compiler
can diagnose calls with incorrect arguments.

The code as posted will probably work with modern compilers (as long as
they permit the old-style "implicit int" rule, dropped by C99), but it's
not a good example for writing new code.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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