Frage zu Pointerarithmetik



  • Ich grüße euch!

    Ich hab da was komisches entdeckt:
    warum ist das :

    short ptr_1 =x;
    *(y++) = ( *(x++) + *(ptr_1++)) ;
    

    nicht das gleiche wie das:

    short ptr_1=x;
    *(y++) = ( *(x++) + *(x++)) ;
    

    Ich hoffe das ihr mir helfen könnt! 😕



  • BrainCoder schrieb:

    warum ist das :

    short ptr_1 =x;
    *(y++) = ( *(x++) + *(ptr_1++)) ;
    

    nicht das gleiche wie das:

    short ptr_1=x;
    *(y++) = ( *(x++) + *(x++)) ;
    

    warum sollte es? unten wird ptr_1 doch gar nicht verwendet.
    🙂



  • ich hab doch

    short *ptr_1=x;
    

    gesetzt! 😕



  • BrainCoder schrieb:

    ich hab doch

    short *ptr_1=x;
    

    gesetzt!

    ja, aber auf deinen zweiten code hat das keine auswirkung. wieso meinst du, dass beides das gleiche sein soll?
    🙂



  • aha ok aber was passiert dann hier:

    void function(short *x, short *y){
    short ptr_1 =x;
    *(y++) = ( *(x++) + *(ptr_1++)) ;}
    

    😕



  • BrainCoder schrieb:

    aha ok aber was passiert dann hier:

    void function(short *x){
    short ptr_1 =x;
    *(y++) = ( *(x++) + *(ptr_1++)) ;}
    

    😕

    das, worauf x zeigt wird geholt, danach wird x hochgezählt. dann wird das, worauf ptr_1 zeigt geholt und ptr_1 wird hochgezählt. beides wird addiert und dorthin geschrieben, worauf y zeigt. dann wird y hochgezählt.
    🙂



  • verstehe aber das ist doch das gleiche wenn ich schreiben würde:

    short ptr_1 =x;
    *(y++) = ( *(x++) + *(x++)//ptr_1 zeigt doch in x oder??) ;
    


  • BrainCoder schrieb:

    verstehe aber das ist doch das gleiche wenn ich schreiben würde:

    short ptr_1 =x;
    *(y++) = ( *(x++) + *(x++)//ptr_1 zeigt doch in x oder??) ;
    
    *(x++) + *(x++)
    

    ist undefiniert, d.h. kann alles mögliche rauskommen.



  • warum undefiniert??

    void function(short *x,*y){
    short ptr_1 =x;
    *(y++) = ( *(x++) + *(x++)) };
    


  • Weil x zweimal verändert wird, ohne dass dazwischen ein Sequenzpunkt ist.



  • BrainCoder schrieb:

    warum undefiniert??

    void function(short *x,*y){
    short ptr_1 =x;
    *(y++) = ( *(x++) + *(x++)) };
    

    6.5.2.4 Postfix increment and decrement operators schrieb:

    The result of the postfix ++ operator is the value of the operand. After the result is
    obtained, the value of the operand is incremented. (That is, the value 1 of the appropriate
    type is added to it.) See the discussions of additive operators and compound assignment
    for information on constraints, types, and conversions and the effects of operations on
    pointers. The side effect of updating the stored value of the operand shall occur between
    the previous and the next sequence point.

    Mit welchem Wert wird das zwaite x evaluiert?


Anmelden zum Antworten