Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do parentheses make a difference when determining the size of an array?

People also ask

What determines the size of an array?

To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.

What does array [] mean?

*array[] means array of pointers, in your example: char *somarray[] = {"Hello"}; somarray[] is array of char* . this array size is one and contains address to on string "Hello" like: somarray[0] -----> "Hello" somarray means address of first element in array. &somarray means array address.

How do you find the size of an array in Java?

To get the size of a Java array, you use the length property. To get the size of an ArrayList, you use the size() method.

Will return the size of the pointer not the array itself?

The sizeof() operator returns pointer size instead of array size. The 'sizeof' operator returns size of a pointer, not of an array, when the array was passed by value to a function. In this code, the A object is an array and the sizeof(A) expression will return value 100.


Whether seemingly redundant parentheses affect the semantics of a program is a long-standing issue in the C standard that still hasn't been adequately resolved.

It is commonly claimed that ((void*)0) is technically not a null pointer constant, because there is no rule that says a parenthesised null pointer constant is a null pointer constant.

Some compilers issue an error for char s[] = ("abc");, because while a character array can be initialised from a string literal, that rule doesn't cover parenthesised string literals.

There are many similar examples. You've found one of them.

From what I can tell, the concensus is basically that the rule should be what C++ does, but what C never formally adopted. C++ makes a parenthesised expression functionally equivalent to the non-parenthesised expression, with a few explicitly-stated exceptions. This would cover all those issues at once.

So technically, the guy could be considered correct, but it's an overly strict interpretation of the standard that nobody really follows, since it's common knowledge that the standard is simply faulty here.


From C99, 6.5.1, on parenthesized expressions:

Its type and value are identical to those of the unparenthesized expression.

At first glance, it would appear that this conflicts with the exception list you're referring to (6.3.2.1):

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type"...

However, this list is in the context of operators/operands; parentheses don't appear to be deemed an operator (based on the categorisation implied by the structure of section 6.5).