Anyone know about this random little feature of C?

Started by
37 comments, last by CJM 19 years, 1 month ago
Quote:Original post by wodinoneeye

Does that work if 'array' is an array of STRUCTs that arent 1 byte (or <= the alignments size) ?????

ex- a struct that is 2 alignment sizes (9-16 if you have it set to 8 byte...)

the [ ] is supposed to multiply the coefficient by the struct size and should point to data in different places in some of the 4 versions shown.

(and thus it only works in certain cases...)


It works properly:
struct Blah{    int a;  //4    char b; //5    int c;  //9    char d; //10};//Padded to 16void SomeFunc(){    Blah* i=0;    int b = 1;    i = b + i;    std::cout << "1 + (Blah*)0 = " << i << std::endl; //"1 + (Blah*)0 = 0x00000010"    i = 0;    i = i + b;    std::cout << "(Blah*)0 + 1 = " << i << std::endl; //"(Blah*)0 + 1 = 0x00000010"}
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Advertisement
Quote:Original post by Oluseyi
Quote:Original post by Machinoid
int i = 'dab';
Won't compile.

Sure it does! 'dab' is a multicharacter literal, which has type int. Completely standard.
Quote:Original post by Polymorphic OOP
Sure it does! 'dab' is a multicharacter literal, which has type int. Completely standard.
Every day, a new reason to despise C and its various bastard offspring.
Did you know that C (and, consequently, C++) allows you to escape newlines in any context? For example, the following is a perfectly valid piece of code:
const char str[] = "foo/bar";int long_var/_name = 0;int funct/ion(unsi/gned int i) {  // works even with /     one-line comments in C99/C++   whi/le(i) {  i -/= 1;  }}


EDIT: Argh, the forum software seems to eat backslashes like there's no tomorrow :( Just pretend that the forward slashes are actually backslashes.

[Edited by - Sharlin on March 3, 2005 3:30:07 AM]
Quote:Original post by bakery2k1
Quote:Original post by ZedFx
There we go, not too hard to work that one out was it, now onto the real dodgy problem:
Quote:first[array]


array[first] ==
*(array + first) ==
*(first + array) ==
first[array]

This works for 2D arrays too:

array[row][col] ==
row[array][col] ==
col[row[array]]
Quote:Original post by Oluseyi
Quote:Original post by Polymorphic OOP
Sure it does! 'dab' is a multicharacter literal, which has type int. Completely standard.
Every day, a new reason to despise C and its various bastard offspring.

You blaspheme! =b I've always liked the freedom of C++. Granted, it's not perfect (far from it) but at least it allows you to apply some imagination unlike BASIC (and all of it's bastard offspring =b).
Quit screwin' around! - Brock Samson
I have yet to find a language that treats the programmer with as much respect as C/C++ do. D looks pretty promising, however; I just wish the tool support was improved.

I'm largely put off by languages with political bents. I'm not interested in converting the world to a specific platform, just in writing great software that is clear, expressive, and easy to understand. Users shouldn't be able to tell that I wrote it in a non-C language (witness Azureus and its reports of grinding computers to a halt - yay garbage collection and massive frameworks!!!) And it should have good IDE support for things that we take for granted, such as Intellisense and whatnot.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Quote:Original post by bakery2k1
Quote:Original post by ZedFx
There we go, not too hard to work that one out was it, now onto the real dodgy problem:
Quote:first[array]


array[first] ==
*(array + first) ==
*(first + array) ==
first[array]


Logically that makes sense, but it's still strange because they obviously had to explicity define the '[]' operator for non-array types that take an array or pointer type as a parameter. And even if it is logical, emotion is telling me that it's weird. :p
Hey,

I'm working off my brain and not a compiler here, but first[array] would not contain the type information that array[first] would, so your compiler should at least warn you when you try to use this syntax. Needless to say, if array was of a type different to int, the code will probably behave strangely on allocation.

Of course, I'm assuming that the compiler implicitly casts from an int to a pointer [void*] in the case of array, and that it implicitly casts from a pointer to an int in the case of first - which I wouldn't have expected it to do without complaining.

Anywho, just some thoughts,

CJM

This topic is closed to new replies.

Advertisement