Kleines C-Rätsel
Frank Schönmann
- sonstiges
hi!
Folgendes C-Programm ist gegeben:
=== cut ===
#include <stdio.h>
int main() {
short a[10];
int b = 5;
a[b] = 42;
printf("%d \n", b[a]);
return 0;
}
=== cut ===
Kommentare? Meinungen? ;)
bye, Frank!
use Mosche;
=== cut ===
#include <stdio.h>
int main() {
short a[10];
int b = 5;
a[b] = 42;
printf("%d \n", b[a]);
return 0;
}
=== cut ===
Kommentare? Meinungen? ;)
Hmm - ich bin kein grosser C-Programmierer (lerne nur ein bißchen an C++ herum), aber schon das reicht mir, um zu sehen, das dieses Codeprogramm _eigentlich_ nicht laufen sollte. Umso erstaunlicher, dass der gcc es selbst mit -Wall ohne Fehlermeldung kompiliert und auch noch das "richtige" *falsche* Ergebnis bei raus kommt.
Da ich mich mit C Internas kaum auskenne, tippe ich mal darauf, dass der gcc (vielleicht noch andere Compiler) b[a] bei nicht vorhandensein von dem Array b automatisch auf a[b] *zeigt*. Aber wie gesagt, ich kenne mich hier kaum aus, bin auf die Lösung gespannt.
Ist das Compiler-Verhalten hier ein Fehler?
use Tschoe qw(Matti);
Moin moin!
Da ich mich mit C Internas kaum auskenne, tippe ich mal darauf, dass der gcc (vielleicht noch andere Compiler) b[a] bei nicht vorhandensein von dem Array b automatisch auf a[b] *zeigt*. Aber wie gesagt, ich kenne mich hier kaum aus, bin auf die Lösung gespannt.
Ja, so ungefaehr. Aus der MCVC5-Hilfe:
One-Dimensional Arrays
A postfix expression followed by an expression in square brackets ([ ]) is a subscripted representation of an element of an array object. A subscript expression represents the value at the address that is expression positions beyond postfix-expression when expressed as
postfix-expression [ expression ]
Usually, the value represented by postfix-expression is a pointer value, such as an array identifier, and expression is an integral value. However, all that is required syntactically is that one of the expressions be of pointer type and the other be of integral type. Thus the integral value could be in the postfix-expression position and the pointer value could be in the brackets in the expression, or "subscript," position. For example, this code is legal:
int sum, *ptr, a[10];
int main()
{
ptr = a;
sum = 4[ptr];
}
Subscript expressions are generally used to refer to array elements, but you can apply a subscript to any pointer. Whatever the order of values, expression must be enclosed in brackets ([ ]).
The subscript expression is evaluated by adding the integral value to the pointer value, then applying the indirection operator (*) to the result. (See Indirection and Address-of Operators for a discussion of the indirection operator.) In effect, for a one-dimensional array, the following four expressions are equivalent, assuming that a is a pointer and b is an integer:
a[b]
*(a + b)
*(b + a)
b[a]
According to the conversion rules for the addition operator (given in Additive Operators), the integral value is converted to an address offset by multiplying it by the length of the type addressed by the pointer.
[...]
Im Prinzip kann man sagen, Summanden sind vertauschbar, und der Compiler sucht sich dan schon raus, welchen der Werte er als Basis nimmt und welchen er mit dem sizeof des basistyps multiplizieren muss.
So long
--
Die Aggressivitaet kommt vom Heavy Metal!!! CounterStrike ist doch nur zum Runterkommen!!!!!
hi!
Im Prinzip kann man sagen, Summanden sind vertauschbar, und der
Compiler sucht sich dan schon raus, welchen der Werte er als Basis
nimmt und welchen er mit dem sizeof des basistyps multiplizieren
muss.
Jepp. In einer C-FAQ hab ich dazu noch folgendes gefunden:
=== cut ===
6.11 I came across some "joke" code containing the "expression"
5["abcdef"] . How can this be legal C?
A: Yes, Virginia, array subscripting is commutative in C. This
curious fact follows from the pointer definition of array
subscripting, namely that a[e] is identical to *((a)+(e)), for *any*
two expressions a and e, as long as one of them is a pointer
expression and one is integral. This unsuspected commutativity is
often mentioned in C texts as if it were something to be proud of,
but it finds no useful application outside of the Obfuscated C
Contest (see question 20.36).
References: Rationale Sec. 3.3.2.1; H&S Sec. 5.4.1 p. 124, Sec. 7.4.1
pp. 186-7.
=== cut ===
Quelle: http://www.faqs.org/faqs/C-faq/faq/.
bye, Frank!