format von nicht dereferenzierten zeigern für die ausgabe



  • hi, wie istn das format von zeigern um sie mir nicht dereferenziert ausgeben zu lassen.

    zweite frage:

    int *test

    test ? x : f

    wieso springt der in den else-teil? welchen wert hat test denn nicht deref.?



  • %p

    Das der in den else Teil springt halte ich für zufällig, da int *test noch keiner Adresse zugewiesen worden ist und somit auf irgendwas zeigt.

    Oder int *test ist eine globale Variable und die wird automatisch mit NULL initialisiert.



  • Das macht Sinn.
    Ich hab hier eine Aufgabe, und eine angebliche Lsg. aber ich weiss nicht wie die darauf kommen.

    char *p1;
    int x;
    float f;

    Aufgabe: Bestimmen Sie den folgenden Ausdruck Typ gemäß.

    p1 ? x : f

    Die Variable wurde nicht initialisiert, noch sonst was. Da müsste der Ausdruck doch int liefern oder nicht? Lösung ist aber angeblich float... wieso?



  • ist *char p1 eine static (oder globale) variable?
    wenn ja, wird p1 mit nullen initialisiert.

    http://c-faq.com/decl/initval.html

    *p1 ist die dereferenzierung und damit der wert an der bezeigten stelle im speicher,
    p1 nur die adresse der bezeigten stelle im speicher.
    das kam nicht so ganz klar rueber, deswegen sag ichs lieber.



  • Es geht doch nur um den Typ des Ausdrucks, der steht zur Compilezeit fest. Die Werte sind dabei egal.

    Wenn der zweite und dritte Operand arithmetische Typen haben, finden die üblichen arithmetischen Konvertierungen statt. In diesem Fall wird int zu float. Der gesamte Ausdruck hat also den Typ float, ist aber kein l-value, wegen der Konvertierung.

    IIRC.



  • du erinnerst dich richtig. kein lvalue, typepromotion in kraft.

    ich bin mal so frei, aus ISO/IEC 9899:1999 zu quoten.
    wenn das verboten ist, mach ich es sofort weg. sollte aber nicht zu tragisch sein, wenn es zu bildungszwecken dient, was es ganz offensichtlich tut.

    6.5.15 Conditional operator
    
        Syntax
    
    1       conditional-expression:
                logical-OR-expression
                logical-OR-expression ? expression : conditional-expression
    
        Constraints
    
    2   The first operand shall have scalar type.
    
    3   One of the following shall hold for the second and third operands:
        — both operands have arithmetic type;
        — both operands have the same structure or union type;
        — both operands have void type;
        — both operands are pointers to qualified or unqualified versions of compatible types;
        — one operand is a pointer and the other is a null pointer constant; or
        — one operand is a pointer to an object or incomplete type and the other is a pointer to a
          qualified or unqualified version of void.
    
        Semantics
    
    4   The first operand is evaluated; there is a sequence point after its evaluation. The second
        operand is evaluated only if the first compares unequal to 0; the third operand is evaluated
        only if the first compares equal to 0; [b]the result is the value of the second or third operand[/b]
        [b](whichever is evaluated), converted to the type described below. 92)[/b] If an attempt is made
        to modify the result of a conditional operator or to access it after the next sequence point,
        the behavior is [b]undefined[/b].
        ____________________
        [b]92) A conditional expression does not yield an lvalue.[/b]
    
    [b]5[/b]   [b]If both the second and third operands have arithmetic type, the result type that would be[/b]
        [b]determined by the usual arithmetic conversions, were they applied to those two operands,[/b]
        [b]is the type of the result.[/b] If both the operands have structure or union type, the result has
        that type. If both operands have void type, the result has void type.
    
    6   If both the second and third operands are pointers or one is a null pointer constant and the
        other is a pointer, the result type is a pointer to a type qualified with all the type qualifiers
        of the types pointed-to by both operands. Furthermore, if both operands are pointers to
        compatible types or to differently qualified versions of compatible types, the result type is
        a pointer to an appropriately qualified version of the composite type; if one operand is a
        null pointer constant, the result has the type of the other operand; otherwise, one operand
        is a pointer to void or a qualified version of void, in which case the result type is a
        pointer to an appropriately qualified version of void.
    
    7   EXAMPLE  The common type that results when the second and third operands are pointers is determined
        in two independent stages. The appropriate qualifiers, for example, do not depend on whether the two
        pointers have compatible types.
    
    8   Given the declarations
    
            const void *c_vp;
            void *vp;
            const int *c_ip;
            volatile int *v_ip;
            int *ip;
            const char *c_cp;
    
        the third column in the following table is the common type that is the result of a conditional expression in
        which the first two columns are the second and third operands (in either order):
    
            c_vp  c_ip  const void *
            v_ip  0     volatile int *
            c_ip  v_ip  const volatile int *
            vp    c_cp  const void *
            ip    c_ip  const int *
            vp    ip    void *
    

Anmelden zum Antworten