[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Re: is the brackets ([]) the comment symbols ?

maykov

6/17/2016 2:26:00 AM

On Friday, December 12, 1997 at 12:00:00 AM UTC-8, Alexey Maykov wrote:
> Hi !
>
> One man told me that the following construction is legal for c:
> int [10] a;
> because square brackets is the kind of the komment symbol in the ansi c
> standard, but VC++5.0 gives a compile error.
> So, the question is, does the man wrong, or i did something wrong ?

Here is what that man said:

a[5] is the same as 5[a]
5 Answers

robertwessel2@yahoo.com

6/17/2016 4:29:00 AM

0

On Thu, 16 Jun 2016 19:26:20 -0700 (PDT), maykov@gmail.com wrote:

>On Friday, December 12, 1997 at 12:00:00 AM UTC-8, Alexey Maykov wrote:
>> Hi !
>>
>> One man told me that the following construction is legal for c:
>> int [10] a;
>> because square brackets is the kind of the komment symbol in the ansi c
>> standard, but VC++5.0 gives a compile error.
>> So, the question is, does the man wrong, or i did something wrong ?
>
>Here is what that man said:
>
>a[5] is the same as 5[a]


It's not true for declarations, so "int 10[a]" doesn't work. OTOH,
when you access an array, you can write:

a[10] = 3;

-or-

10[a] = 3;

and accomplish the same thing. That's because the array accessor
essentially decays into a pointer expression. IOW, the two above
lines are equivalent to:

*(a+10) = 3;

-and-

*(10+a) = 3;

The later form ("10[a]") is primarily used to confuse newbies.

But it's in no way a comment.

David Brown

6/17/2016 7:52:00 AM

0

On 17/06/16 04:26, maykov@gmail.com wrote:
> On Friday, December 12, 1997 at 12:00:00 AM UTC-8, Alexey Maykov wrote:
>> Hi !
>>
>> One man told me that the following construction is legal for c:
>> int [10] a;
>> because square brackets is the kind of the komment symbol in the ansi c
>> standard, but VC++5.0 gives a compile error.
>> So, the question is, does the man wrong, or i did something wrong ?
>
> Here is what that man said:
>
> a[5] is the same as 5[a]
>

True (within the right context). But that was considered an unfortunate
quirk of the language and very bad style 20 years ago when this thread
was started - it is even more so now.

It is good to be helpful and try to answer questions on Usenet, but I
doubt that the OP here has been holding his breath for the last two
decades, waiting for an answer!

Ian Collins

6/17/2016 10:48:00 AM

0

On 06/17/16 02:26 PM, maykov@gmail.com wrote:
> On Friday, December 12, 1997 at 12:00:00 AM UTC-8, Alexey Maykov wrote:

Time travel again...

--
Ian

Real Troll

6/17/2016 5:22:00 PM

0

On 17/06/2016 08:51, David Brown wrote:
> It is good to be helpful and try to answer questions on Usenet, but I
> doubt that the OP here has been holding his breath for the last two
> decades, waiting for an answer!

He must have died by now!!!!!!!!!!

legalize+jeeves

6/17/2016 5:35:00 PM

0

[Please do not mail me a copy of your followup]

As others have already mentioned swapping the name of an array and
it's index may be valid syntax, but it is not recommended. Just because
something is allowed doesn't mean it's a good idea.

maykov@gmail.com spake the secret code
<51b8cc80-a55c-4134-900b-a820d18e099b@googlegroups.com> thusly:

>Here is what that man said:
>
>a[5] is the same as 5[a]

For expressions in the C language, yes.

1 #include <stdio.h>
2
3 int main()
4 {
5 int a[1] = { 0 };
6 0[a] = 10;
7 printf("%d\n", a[0]);
8 return 0;
9 }

> gcc /tmp/a.c
> ./a.out
10

For expressions in the C++ language, no.

1 #include <iostream>
2 #include <vector>
3
4 int main()
5 {
6 std::vector<int> a(1);
7 0[a] = 10;
8 std::cout << a[0] << '\n';
9 return 0;
10 }

> g++ /tmp/a.cpp -o a2.out
/tmp/a.cpp: In function 'int main()':
/tmp/a.cpp:7:4: error: no match for 'operator[]' (operand types are 'int' and 'std::vector<int>')
0[a] = 10;
^
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pi...
The Computer Graphics Museum <http://computergraphicsmuse...
The Terminals Wiki <http://terminals.classicc...
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpre...