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[i] 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. 10 1526
hari4063 <za**************@pmf.unsa.ba> wrote: 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
--
Florian Weingarten | IRCnet: fw (fw_) | PGP: 0x65C91285 http://hackvalue.de/~flo/ | ICQ: 38564900 | Jabber: fw@jabber.ccc.de
hari4063 wrote: 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] = i; At first I think that is some compiler specific _hack_, but I found something that is called "array commutation"? So, using a[i] 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?
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cl**********@nntp1.jpl.nasa.gov... hari4063 wrote:
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] = i;
This will result in the exact same behavior as that
of the code above.
-Mike
"hari4063" <za**************@pmf.unsa.ba> wrote in message
news:56******************************@localhost.ta lkaboutprogramming.com... 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[i] 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
On Fri, 29 Oct 2004 17:09:02 -0700, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote: hari4063 wrote:
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] = 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.)
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?
In article <44******************************@localhost.talkab outprogramming.com>,
hari4063 <za**************@pmf.unsa.ba> wrote: 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[i].
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]);
}
--------
Unless there's a good reason not to use an array of structs, though,
this can be improved even more by doing so:
--------
for(i=0;i<nelems;i++)
{
arr[i].output=mangle(arr[i].input1,arr[i].input2);
}
--------
dave
--
Dave Vandervies dj******@csclub.uwaterloo.ca
[They think] that spinning your tires madly makes you accelerate faster. When
they catch up to my minivan at the next light, I try to explain this fallacy of
their thinking to them as well. --Paul Tomko in the scary devil monastery
On Sat, 30 Oct 2004 19:08:06 -0400, "hari4063"
<za**************@pmf.unsa.ba> wrote: 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.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
In article <5q********************************@4ax.com>, Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes: 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 mi************@microfocus.com
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
On 1 Nov 2004 17:14:19 GMT, mw*****@newsguy.com (Michael Wojcik) wrote: In article <5q********************************@4ax.com>, Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes: "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...
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Doug Farrell |
last post by:
Hi all,
I'm trying to do the following from within a code module:
import re
# text to match
text = "Good morning x something /x, how are you today x something
else /x"
|
by: Mathias Mamsch |
last post by:
Hi,
I have some confusion concerning the weakref module.
I am trying to save a weak reference to a bound member function of a class
instance for using it as a callback function. But I always...
|
by: Grant Edwards |
last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273
Alan Kay said something I really liked, and I think it applies
equally well to Python as well as the languages...
|
by: Kamilche |
last post by:
I'm trying to pack two characters into a single byte, and the shifting
in Python has me confused.
Essentially, it should be possible to use a 'packed string' format in
Python, where as long as...
|
by: JMCN |
last post by:
object invalid or no longer set - confusion of the recordset in access
2003.
i am currently converting from access 97 to access 2003. majority of
the codes converted over perfectly fine, though...
|
by: i_have_control |
last post by:
I'd be grateful for any input on this one:
I have three web domains. The destinations of two are set to folders
on the first, though that fact is transparent to the user (i.e: it does
not...
|
by: Steve |
last post by:
I have a form with a dataset and a datagrid.
I created a dataview on this dataset.
When the user modifies the datagrid, I look up this record in the dataview
to make sure it is unique.
Here is...
|
by: joelagnel |
last post by:
hi friends,
i've been having this confusion for about a year, i want to know the
exact difference between text and binary files.
using the fwrite function in c, i wrote 2 bytes of integers in...
|
by: Richard Lewis Haggard |
last post by:
I'm having a problem with what appears to be some sort of confusion with
references. I have a single solution with a dozen projects which has been
working quite nicely for a while. The references...
|
by: Riaaaa |
last post by:
Hello,
We are doing the project in VB.Net.
We had a great confusion for ASP.Net and VB.Net.
Is VB.Net project performed in Microsoft Visual Studio 2005 ??
We have...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |