PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : array und pointer ist es nicht das gleiche???



barton4
23-08-2006, 10:43
auf: http://www.codeguru.com/forum/showthread.php?t=231162 stehen ein paar häufig gemachte fheler in c und c++ ...

Doch bei dem code:

char *s= "blabla"
s[1]='S'

blick ich nicht durch weil, es ergibt dann ein segemnt fault,habe bisher angenommen das [] und * im endefekt die
gleihe bedeutung haben z.B

z.B kann man ja wenn ich mich recht entsinnen kann:
void main(int argc, char *argv[]) oder
void main(int argc, char **argv) oder
void main(int argc, char argv[][])
haben ja auch die gleiche bedeutung
???...

locus vivendi
23-08-2006, 11:10
Nein, Arrays und Pointer sind nicht das gleiche. Und in Stringliterale darf man nicht schreiben. Deine Quelle ist korrekt.


Doch bei dem code:

char *s= "blabla"
s[1]='S'

blick ich nicht durch weil, es ergibt dann ein segemnt fault, [...]
Es gibt nicht zwangläufig einen Segmentation-Fault. Aber es ist erlaubt das das passiert. Übrigens ist das man überhaupt char* s = "..." schreiben darf nur ein doofes Überbleibsel von C. Man sollte besser char const* s = "..." benutzen, weil der Compiler dann solche Fehler abfängt.


,habe bisher angenommen das [] und * im endefekt die
gleihe bedeutung haben z.B [...]
Mal davon abgesehen ob deine Annahme zutrifft oder nicht hat das nicht allzuviel mit dem oberen Teil zutun. Die Elemente eines Stringliterals sind konstant, deshalb geht obiges nicht.

Thomas Engelke
23-08-2006, 14:21
Aus dem comp.lang.c-FAQ, Abteilung Array vs. Pointer (http://c-faq.com/aryptr/index.html):


Q: But I heard that char a[] was identical to char *a.

A: Not at all. (What you heard has to do with formal parameters to functions; see question 6.4.) Arrays are not pointers, though they are closely related (see question 6.3) and can be used similarly (see questions 4.1, 6.8, 6.10, and 6.14).

...

It is useful to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location ``p'', fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In other words, a[3] is three places past (the start of) the object named a, while p[3] is three places past the object pointed to by p. In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently. (The essential difference is that the values of an array like a and a pointer like p are computed differently whenever they appear in expressions, whether or not they are being subscripted, as explained further in question 6.3.) See also question 1.32.

Die FAQ macht viel Spaß. Besonders über Array vs. Pointer gibt's ellenlange Diskussionen, gerade weil das Thema immer wieder hinterfragt wird. Glücklicherweise kann die FAQ dieses jedoch zur Genüge erklären.

TME