Declaring a variable after a list of function arguments

I messed up the C code where the variables seem to be declared after the function argument list. I did not know that this is possible.

In the code, int r and int *a are declared immediately after the argument list arr2int . How does this affect the local variables a and r of arr2int ?

The actual reason I want to understand the code is because if I let it run on my Linux x86 a[1] = 1 in //MARKER when arr2int ist is called first.

But if I allow it to work on the basis of omap4 dev a[1]=0 on the basis of ARM, and I do not know why there is a difference.

Could you comment on this?

 long arr2int(a,r) int r; int *a; { int i; long mul, result = 0, temp; //MARKER for (i=1; i<=r; i++) { mul = 1; temp = a[i]-1; while (temp--) mul = mul << 1; result += mul; } return(result); } 

And the calling method:

 void generateEncodingTable(){ register int i,j; long temp; int seed = 133757; printf("\n[I] - Generating Encoding Table\n"); for (pattern = 0; pattern < 4096; pattern++) { temp = pattern << 11; /* multiply information by X^{11} */ encoding_table[pattern] = temp + get_syndrome(temp);/* add redundancy */ } decoding_table[0] = 0; decoding_table[1] = 1; temp = 1; for (i=2; i<= 23; i++) { temp *= 2; decoding_table[get_syndrome(temp)] = temp; } a[1] = 1; a[2] = 2; temp = arr2int(a,2); decoding_table[get_syndrome(temp)] = temp; for (i=1; i<253; i++) { nextcomb(23,2,a); temp = arr2int(a,2); decoding_table[get_syndrome(temp)] = temp; } a[1] = 1; a[2] = 2; a[3] = 3; temp = arr2int(a,3); decoding_table[get_syndrome(temp)] = temp; for (i=1; i<1771; i++) { nextcomb(23,3,a); temp = arr2int(a,3); decoding_table[get_syndrome(temp)] = temp; } } 
+4
source share
4 answers

This is the old notation known as K and R (Kernigan and Ritchie, after Brian Kernigan and Dennis Ritchie) Designation for declaring functions. If your compiler supports it, you can use it, and it is similar to declaring a function with ANSI musical notation.

+4
source

As others mention, this is an early style coding function.

The following is a trap of this style. Remember that checking passed parameters does not exist. . This may explain your run-time differences.

Say you declare a function

 int foo(a,b,c); 

All at this point the compiler sees a function called "foo" that takes 3 arguments and returns an int . Thus, usage verification is limited to this.

Suppose that sizeof(short) < sizeof(int) < sizeof(long) and the function is defined as

 int foo(a,b,c) int a; long b; int c; { /* code body */ } 

Note the following use of foo

 int d,e,f; d = foo(1,2L,3); e = foo((short)1,2L,3); f = foo(1,2,3); 

The first customs work fine, the correct integer values ​​are passed to foo .
2nd customs also work great. The first argument, up to the size of int before calling, like printf("%d", (short)2) , advances (short)2 to int before going to printf() .
The third problem, because the compiler does not know that the second argument must be long . Thus, the data passed to foo is not transferred correctly. β†’ UB

+4
source

This is the old C syntax. If your compiler can learn it, it should work just as if you had declared functions in the usual, ANSI way.

+2
source

This is K & RC, that is, the C language described in the first issue of the C programming language by Brian Kernigan and Dennis Ritchie. The second edition turned to ANSI C (C89).

You always use the ANSI C style:

Justification for the International Standard - Programming Languages ​​C 6.11.6 Function Declaration

The characterization as obsolete from the use of declarations of the β€œold style” function and definition, that is, the traditional style that does not use prototypes, signals to the Committees that the new prototype style should eventually replace the old style.

The essence of this case is that the new syntax touches on some of the most egregious flaws of the language defined in K & R, that the new style surpasses the old style for each count.

+2
source

Source: https://habr.com/ru/post/1494732/


All Articles