Confusion with C

H

hari4063

Every day I prove that i am beginer with C (even I program in C or/and C++
for more than 5 years)! Some days ago I find code that use _REALY_ strange
sintax, something like this:

// ------- code ------
int a[10];
int i;
for(i=0; i<10; i++)
i[a] = i;
// --- end ------------

At first I think that is some compiler specific _hack_, but i find
something that is called "array comutation"? So, using a is same like
*((a)+(i)). With this, upper code have (just little bit) logic.

My questiona are:
1) is this portable
2) is this truely what C creators wanna to have, or just parser use this
becuase *(a+i) rule?

Thanks.
 
F

Florian Weingarten

hari4063 said:
My questiona are:
1) is this portable
2) is this truely what C creators wanna to have, or just parser use this
becuase *(a+i) rule?

ISO/IEC 9899:1999 (E), 6.5.2.1 Array subscripting

2 A postfix expression followed by an expression in square brackets [] is a
subscripted designation of an element of an array object. The definition
of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).
Because of the conversion rules that apply to the binary + operator, if E1
is an array object (equivalently, a pointer to the initial element of an
array object) and E2 is an integer, E1[E2] designates the E2-th element of
E1 (counting from zero).


Flo
 
E

E. Robert Tisdale

hari4063 said:
Every day, I prove that I am beginner with C
(even I program in C or/and C++ for more than 5 years)!
Some days ago I found code that uses _REALLY_ strange syntax,
something like this:
// ------- code ------
int a[10];
int i;
for(i=0; i<10; i++)
i[a] = i;
// --- end ------------

This is bad code. You should write:

int a[10];
for (int i = 0; i < 10; ++i)
a = i;
At first I think that is some compiler specific _hack_,
but I found something that is called "array commutation"?
So, using a is same like *((a)+(i)).
With this, upper code have (just little bit) logic.

My questions are,
1) "Is this portable?",
2) "Is this truely what C creators wanna to have?"
"Or just parser use this because *(a+i) rule?"

It is portable.
The language designers meant to have this flexibility.
You code doesn't show any motivation for "array commutation".
Why didn't you post the code that you found?
 
M

Mike Wahler

E. Robert Tisdale said:
hari4063 said:
Every day, I prove that I am beginner with C
(even I program in C or/and C++ for more than 5 years)!
Some days ago I found code that uses _REALLY_ strange syntax,
something like this:
// ------- code ------
int a[10];
int i;
for(i=0; i<10; i++)
i[a] = i;
// --- end ------------

This is bad code.

It's perfectly valid.
You should write:

int a[10];
for (int i = 0; i < 10; ++i)
a = i;


This will result in the exact same behavior as that
of the code above.

-Mike
 
M

Mike Wahler

hari4063 said:
Every day I prove that i am beginer with C (even I program in C or/and C++
for more than 5 years)! Some days ago I find code that use _REALY_ strange
sintax, something like this:

// ------- code ------
int a[10];
int i;
for(i=0; i<10; i++)
i[a] = i;
// --- end ------------

At first I think that is some compiler specific _hack_, but i find
something that is called "array comutation"? So, using a is same like
*((a)+(i)).


Yes. Also the same as i[a]
With this, upper code have (just little bit) logic.

My questiona are:
1) is this portable
Yes.

2) is this truely what C creators wanna to have, or just parser use this
becuase *(a+i) rule?

http://www.eskimo.com/~scs/C-faq/s6.html
http://www.eskimo.com/~scs/C-faq/q6.11.html

-Mike
 
R

Raymond Martineau

hari4063 said:
Every day, I prove that I am beginner with C
(even I program in C or/and C++ for more than 5 years)!
Some days ago I found code that uses _REALLY_ strange syntax,
something like this:
// ------- code ------
int a[10];
int i;
for(i=0; i<10; i++)
i[a] = i;
// --- end ------------

This is bad code. You should write:
int a[10];
for (int i = 0; i < 10; ++i)
a = i;


In the older C compilers, variables may only be initialized at the top of a
block. Placing it within the for-loop (or after a regular statement)
causes a compilation error.

Also, changing the 'i++' around is absolutely unnecessairy in C:
http://www.eskimo.com/~scs/C-faq/q3.12.html
(Mabye in C++, but not C.)
 
H

hari4063

Thanks to all. I find this code in obfuscated context :) Another question :
is there any problem/algo/whatever that use this sintax and that can not be
written with (let say) normal array subscription?
 
D

Dave Vandervies

Thanks to all. I find this code in obfuscated context :) Another question :
is there any problem/algo/whatever that use this sintax and that can not be
written with (let say) normal array subscription?

No, because any use of i[a] can trivially be rewritten as a.

A more interesting question is whether there's anywhere where this might
be useful, f'rexample by making code clearer.

I asked about that a while ago, and one reply mentioned that if you're
storing related values as a set of arrays rather than as an array of
structs, reversing the normal indexing might make that a bit clearer:
--------
for(i=0;i<nelems;i++)
{
/*Doing things this way emphasizes the "field names" that we're
interested in
*/
i[output]=mangle(i[input1],i[input2]);
}
 
K

Kevin D. Quitt

Thanks to all. I find this code in obfuscated context :) Another question :
is there any problem/algo/whatever that use this sintax and that can not be
written with (let say) normal array subscription?

There is nothing that cannot be done only by one method because the
methods aren't just similar, they're functionally and syntactically
identical. Other than for obfuscated code, I have found only one use for
the 'inverted' subscript: portable and obvious conversion from an index to
a hex digit.

"0123456789ABCDEF"[ i ]

where it is known to be in the range of 0 through 15. And even this use
is merely cute; it's not easier to read (or harder). It does avoid the
problem of

HexChars[ i ]

where HexChars is a string defined elsewhere that might have been changed
- but that's not really a legitimate excuse.
 
M

Michael Wojcik

Other than for obfuscated code, I have found only one use for
the 'inverted' subscript: portable and obvious conversion from an index to
a hex digit.

"0123456789ABCDEF"[ i ]

where it is known to be in the range of 0 through 15.

But that's a normal use of array notation, with the subscript in the
square brackets. The "inverted" usage would be:

i["0123456789ABCDEF"]

If "i" is replaced with any sort of more complex expression, though,
putting it in the square brackets is clearly better, since it saves
having to introduce parentheses. For example, if we don't want to
assume i is in the domain:

"0123456789ABCDEF"[i & 0xf]

versus

(i & 0xf)["0123456789ABCDEF"]

Of course, none of these are likely to come up all that often. (I
have boilerplate "hex dump" code I use in various projects, and I
bet many others here do, but aside from that how often does the
literal string array syntax trick appear?)

--
Michael Wojcik (e-mail address removed)

The lark is exclusively a Soviet bird. The lark does not like the
other countries, and lets its harmonious song be heard only over the
fields made fertile by the collective labor of the citizens of the
happy land of the Soviets. -- D. Bleiman
 
K

Kevin D. Quitt

"0123456789ABCDEF"[ i ]
But that's a normal use of array notation, with the subscript in the
square brackets.

Of course. I knew that, but I had a really good reason for putting it
that way. Honest. Would a magician lie to you? Well, yes, but that's
another story...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top