Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semicolon on a function parameters

Tags:

c++

c

syntax

matrix

matrix_* matrix_insert_values(int n; double a[][n], int m, int n) {     matrix_* x = matrix_new(m, n);     for (int i = 0; i < m; i++)         for (int j = 0; j < n; j++)             x->v[i][j] = a[i][j];     return x; } 

Example of my test matrix

double in[][3] = {     { 12, -51,   4},     {  6, 167, -68},     { -4,  24, -41},     { -1, 1, 0},     { 2, 0, 3}, }; 

I'm a bit lost, I can't figure out what int n; is inside my argument declaration, it works over C but C++ doesn't allow this implementation. I want to understand how this is working because I'm going to migrate this code to C++.

like image 860
Gerard Avatar asked Feb 28 '13 20:02

Gerard


People also ask

Do you put semicolon after function?

Semicolons after function declarations are not necessary. There's no semicolon grammatically required, but might wonder why? Semicolons serve to separate statements from each other, and a FunctionDeclaration is not a statement.

What does a semicolon mean in logic?

In particular, it seems quite clear that the semicolon means "and" in the example you are interested in (the second part of the definition seems to not even make sense without assuming the first part, since "no other node" seems to mean "no node other than the one referred to in the previous statement).

What does colon mean in function?

1. To denote ratio or odds, as in 2:1 (voiced "two to one"). 2. To mean such that in constructions such as (voiced "the set of numbers such that ). 3.

Does function prototype end with a semicolon?

The only difference between the function prototype and the function header is a semicolon (see diagram below). The function definition is placed AFTER the end of the int main(void) function. The function definition consists of the function header and its body.


1 Answers

It is a seldom-used feature from C99 GNU extension (GCC documentation) that is used to forward-declare parameters used in VLA declarators.

matrix_* matrix_insert_values(int n; double a[][n], int m, int n); 

Do you see how int n appears twice? The first int n; is just a forward declaration of the actual int n, which is at the end. It has to appear before double a[][n] because n is used in the declaration of a. If you were okay with rearranging parameters, you could just put n before a and then you wouldn't need this feature

matrix_* matrix_insert_values_rearranged(int m, int n, double a[][n]); 

Note about C++ compatibility

To be clear, the GNU extension is just the forward declaration of function parameters. The following prototype is standard C:

// standard C, but invalid C++ matrix_* matrix_insert_values_2(int m, int n, double a[][n]); 

You cannot call this function from C++, because this code uses variable length arrays, which are not supported in C++. You would have to rewrite the function in order to be able to call it from C++.

like image 54
Dietrich Epp Avatar answered Sep 29 '22 13:09

Dietrich Epp