Weird (?) explicit declaration of functions

  • Thread starter Nils Emil P. Larsen
  • Start date
N

Nils Emil P. Larsen

Hello

I have read about a C shared library which I want to use in my C
program. (It's a library to encode/decode packets from/to a serial bus
running with the SNAP-protocol).

Unfortunatly there is no source code available, just a C file with
function declarations like:

long SendData(TXData)Type_TXData *TXData;
Void GetStatistics(Stat)Type_Statistics *Stat;

My problem is, I don't understand what it means. Is it equivalent to
long SendData(Type_TXData *TXData);
- which makes a lot more sense to me?

Is there a good reason to use this strange syntax?

Nils Emil
 
J

Joona I Palaste

Nils Emil P. Larsen said:
I have read about a C shared library which I want to use in my C
program. (It's a library to encode/decode packets from/to a serial bus
running with the SNAP-protocol).
Unfortunatly there is no source code available, just a C file with
function declarations like:
long SendData(TXData)Type_TXData *TXData;
Void GetStatistics(Stat)Type_Statistics *Stat;
My problem is, I don't understand what it means. Is it equivalent to
long SendData(Type_TXData *TXData);
- which makes a lot more sense to me?

It appears to be a K&R1-style declaration, which predates ISO C, and is
now considered obsolete.
Is there a good reason to use this strange syntax?

No, if not for legacy compability.
 
C

Chris Torek

It appears to be a K&R1-style declaration, which predates ISO C, and is
now considered obsolete.

While:

Tfunc funcname(arg1, arg2)
Targ1 arg1;
Targ2 arg2;

is indeed K&R-1 ("pre-ANSI" or "Classic C") syntax, it is not valid
as a *declaration*, only as a *definition*, and it must be followed
by the open brace of the function, e.g.:

int main(argc, argv)
int argc;
char **argv;
{
/* local variables here */
...
return 0;
}

The parameters "argc" and "argv" become local (block-scope) variables
inside the function in the usual way, just as with prototype-syntax
parameters. (The fact that the scope of argc and argv is the same as
that of any block-scope variable declared right after the "{" is the
reason I eventualy adopted the indentation shown above, for K&R-1 C.)

At least one K&R-style compiler would, if given something like the
above as a declaration, do something very bad:

void f1(arg) int arg; /* ERROR -- no function body */
char *f2(arg) char *arg; /* ERROR -- no function body */
int main(argc, argv) int argc; char **argv; { ... }

Here the variables "argc" and "argv" would exist as one expected,
but have the wrong internal addresses, and the stack would get
wrecked.
No, if not for legacy compability.

Indeed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top