Why Does C++ Allow Us to Surround the Variable Name in Parentheses When Declaring a Variable

Why does C++ allow us to surround the variable name in parentheses when declaring a variable?

Grouping.

As a particular example, consider that you can declare a variable of function type such as

int f(int);

Now, how would you declare a pointer to such a thing?

int *f(int);

Nope, doesn't work! This is interpreted as a function returning int*. You need to add in the parentheses to make it parse the right way:

int (*f)(int);

The same deal with arrays:

int *x[5];   // array of five int*
int (*x)[5]; // pointer to array of five int

Why should I wrap variable name inside parenthesis in C programming?

So why we need to declared the type like : int check(int a) { ... }

From this it is clear you're referring to the function and not to the variable itself. That part of the code is defining a function.

So, let's separate the two things in your code:

int (a);

Declares variable a. As @eyl327 stated:

There is no difference between int (a); and int a;

So that int could be called whatever you wanted; that it now has the same name as the variable used in the function definition is just coincidence. It could be, for example, int my_integer;.

int check(a) {
return a % 2;
}

Here you define a function which receives an integer and which will return modulo 2 of that int (you can read this if you don't know what that means).

It is missing the type of the variable which receives the function. It should be, instead:

int check(int a) {
return a % 2;
}

Now, I think it is possible that the combination used in your code is valid, but I've never seen it before, and if you're learning now, I'd suggest you get familiar with the traditional way of defining variables and functions.

Hope this helps!

why does C++ allow a declaration with no space between the type and a parenthesized variable name?

One requirement when defining the syntax of a language is that elements of the language can be separated. According to the C++ syntax rules, a space separates things. But also according to the C++ syntax rules, parentheses also separate things.

When C++ is compiled, the first step is the parsing. And one of the first steps of the parsing is separating all the elements of the language. Often this step is called tokenizing or lexing. But this is just the technical background. The user does not have to know this. He or she only has to know that things in C++ must be clearly separted from each others, so that there is a sequence "*", "Oit", "(", "bar", ")", "=", "7", ";".

As explained, the rule that the parenthesis always separates is established on a very low level of the compiler. The compiler determines even before knowing what the purpose of the parenthesis is, that a parenthesis separates things. And therefore an extra space would be redundant.

When you ever use parser generators, you will see that most of them just ignore spaces. That means, when the lexer has produced the list of tokens, the spaces do not exist any more. See above in the list. There are no spaces any more. So you have no chance to specify something that explicitly requires a space.

Which part of the C++ standard allow to declare variable in parenthesis?

[dcl.meaning] in the Standard says:

In a declaration T D where D has the form ( D1 ) the type of the contained declarator-id is the same as that of the contained declarator-id in the declaration T D1.

Parentheses do not alter the type of the embedded declarator-id, but they can alter the binding of complex declarators.

More simply, you can put parentheses around anything considered a "declarator" in the C++ grammar. (Loosely speaking, a declarator is a part of a declaration without the initial specifiers and types which contains one name.)

In your example, the identifier s is a declarator, so you're allowed to put parentheses around it and the meaning doesn't change.

The reason for this, as the second quoted sentence hints, is that it can be necessary when things get more complicated. One example:

int * a [10];     // a is an array of ten pointers to int.
int ( * b ) [10]; // b is a pointer to an array of ten ints.

C++ - Parentheses after variable name in declaration

Having had a look at the rest of the file, I believe you are looking at some C++ code.

CRGBPalette16 currentPalette( CRGB::Black );

This line initialises an object of type CRGBPalette16 with the enum parameter CRGB::Black.

You can see the object passed as a reference into other functions, such as here on line 72:

leds[i] = ColorFromPalette( currentPalette, colorIndex + sin8(i*16), brightness);

What do parentheses in a C variable declaration mean?

What are the parentheses for?

In C brackets [] have a higher precedence than the asterisk *

Good explanation from Wikipedia:

To declare a variable as being a
pointer to an array, we must make use
of parentheses. This is because in C
brackets ([]) have higher precedence
than the asterisk (*). So if we wish to declare a pointer to an array, we need to supply parentheses to override this:

double (*elephant)[20];

This declares that elephant is a
pointer, and the type it points at is
an array of 20 double values.

To declare a pointer to an array of
pointers, simply combine the
notations.

int *(*crocodile)[15];

Source.

And your actual case:

int (*data[2])[5];

data is an array of 2 elements. Each element contains a pointer to an array of 5 ints.

So you you could have in code using your 'data' type:

int (*data[2])[5];
int x1[5];
data[0] = &x1;
data[1] = &x1;

data[2] = &x1;//<--- out of bounds, crash data has no 3rd element
int y1[10];
data[0] = &y1;//<--- compiling error, each element of data must point to an int[5] not an int[10]

ERROR: Cannot use parentheses when declaring variable with deduced class template specialization type

clang's error message is oddly specific – g++ and vc++ only complain about conflicting declarations of v– but note that it says "declaring a variable".

Since func is a type, and you can have parentheses around the identifier in a variable declaration, and because of the "if it can be interpreted as a declaration, it is a declaration" rule,

func(v);

is equivalent to

func v;

declaring a variable v of the type func.

Here is a shorter example of the same situation:

#include <vector>

int main() {
int v = 0;
std::vector<int>(v);
}


Related Topics



Leave a reply



Submit