-2

Duplicate: Alternative (K&R) C syntax for function declaration versus prototypes

I have some homework I need to do (in C) and in this homework we're given some method stubs. I'm not very good in C, but when I looked at the stubs this caught my eye:

...
A_output(message)
  struct msg message;
{

}

 A_input(packet)
  struct pkt packet;
{

}
...

Namely, the definition of both msg and pkt between the function's name/parameter and definition. Both the messageand packet parameters are to be structs; their respective member declarations are above their first uses, towards the beginning of the .c file:

struct msg {
  char data[20];
  };

struct pkt {
   int seqnum;
   int acknum;
   int checksum;
   char payload[20];
    };

Is this kind of initialization some sort of C syntax I'm not familiar with or just a typo? I looked around but couldn't find anything like this, nor have I ever seen anything like it in any other language, to split up a function's code like that. The compiler is screaming about invalid function prototyping no matter what I do so something seems amiss...

parttimeturtle
  • 1,125
  • 7
  • 22
  • 4
    Oh, god, it's the 70s all over again. Implicit int and K&R functions. – DeiDei Apr 16 '17 at 08:47
  • 1
    "The compiler is screaming about invalid function prototyping" -- Are you sure you're using a C compiler? (Honest question. C++ compilers won't like it.) –  Apr 16 '17 at 08:50
  • Yes, I've been compiling with GCC. – parttimeturtle Apr 16 '17 at 08:51
  • 1
    https://stackoverflow.com/questions/3092006/function-declaration-kr-vs-ansi – Ry- Apr 16 '17 at 08:53
  • @hvd even the source of gcc is filled with these kinds of definitions. – Ajay Brahmakshatriya Apr 16 '17 at 08:55
  • Possible duplicate of [Data declaration in strange C code](http://stackoverflow.com/questions/37531010/data-declaration-in-strange-c-code) – too honest for this site Apr 16 '17 at 08:59
  • @AjayBrahmakshatriya GCC is a special case. For a long time, GCC went out of its way to ensure it itself could be compiled with pre-ANSI C compilers. After all, if you don't have an ANSI C compiler on a system, and the only ANSI C compiler you can find requires you to have an ANSI C compiler already, you're stuck. –  Apr 16 '17 at 09:01
  • That's invalid code since 18 years now (and deprecated since 28 years). – too honest for this site Apr 16 '17 at 09:02
  • @hvd: So you say the code above is **without a change** valid standard C? Despite the implicit `int`? (not related, but fyi: non-prototype declarators are obsolescence features) – too honest for this site Apr 16 '17 at 09:07
  • There is no `struct` (or any other) definition in the function header. – too honest for this site Apr 16 '17 at 09:15
  • @Olaf You're right, what I wrote was wrong. The OP asks about the non-prototyped declaration, but uses implicit `int` in the code as well. The former is still valid, the latter is not. I thought you were saying the former is no longer valid, but you didn't say that. –  Apr 16 '17 at 09:24
  • @hvd: I did not intentionally. Finding out and understanding **what** the actual problem is is a good lesson for beginners. – too honest for this site Apr 16 '17 at 09:27
  • @parttimeturtle: C99 is not standard C. But - to be fair - this has not changed in 2011 (I'm just not sure about making K&R parameter _declarations_ an obsolescence feature; maybe you can find out). Said that, you **always** enable compiler warnings. Without ever run into this myself, I'm confident there is an option which covers non-prototype functions declarators. – too honest for this site Apr 16 '17 at 09:30

1 Answers1

0

That is very old code, from before it was standardized in the 1980's.

A_output(message)
  struct msg message;
{

}

Is a function definition, and message is the argument. That's how arguments were defined in "the olden days". Note the missing return type, it implies that the function returns an int.

The above definition is equivalent to the following:

int A_output(struct msg message)
{

}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621