Thread: typedef problem

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    15

    Question typedef problem

    hi everyone!

    what is wrong with this declaration?:

    Code:
    enum tKoerperart { einfach, komplex };
    enum tGeometrie { wuerfel, kugel, zylinder, kegel, tetraeder };
    ...
    typedef struct koerper{
          enum tKoerperart art;      
          enum tGeometrie typ;
          ...
    }tKoerper; 
    
    typedef tKoerper *pKoerper;
    i can not assign values to variables of this type.
    Code:
      tObjekt objekt;
      ...
      objekt.koerper->art=einfach;
      objekt.koerper->typ=kugel;
      ...
    what am i doing wrong here?
    greets, and by the way:"merry x-mass"

  2. #2
    Quote Originally Posted by dr$brown
    hi everyone!

    what is wrong with this declaration?:

    Code:
    enum tKoerperart { einfach, komplex };
    enum tGeometrie { wuerfel, kugel, zylinder, kegel, tetraeder };
    ...
    typedef struct koerper{
          enum tKoerperart art;      
          enum tGeometrie typ;
          ...
    }tKoerper; 
    
    typedef tKoerper *pKoerper;
    i can not assign values to variables of this type.
    How do you know ? Do you have some compile error or is it an execution problem? 'objekt.koerper' seems to be a pointer. Is it correctly initialised ?
    Code:
      tObjekt objekt;
      ...
      objekt.koerper->art=einfach;
      objekt.koerper->typ=kugel;
      ...
    what am i doing wrong here?
    You din't show us the definition of 'tObjekt'. The way the structure is initialized was missing too...
    Emmanuel Delahaye

    "C is a sharp tool"

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    sorry, here it is:
    Code:
    typedef tKoerper *pKoerper;
    
    typedef struct objekt {
      ...
      pKoerper koerper;
      ...
    }tObjekt;

  4. #4
    Quote Originally Posted by dr$brown
    sorry, here it is:
    Code:
    typedef tKoerper *pKoerper;
    
    typedef struct objekt {
      ...
      pKoerper koerper;
      ...
    }tObjekt;
    Hiding pointers is considered bad practice. I ask again, how was the 'koerper' pointer initialized ?
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    15
    so far i have no initialization for the pointer, but even using a struct it doesnt work.
    what is >>Hiding pointers<< ?

  6. #6
    Quote Originally Posted by dr$brown
    so far i have no initialization for the pointer, but
    You can't dereference a pointer that has not been correctly initialized. It invokes an undefined behaviour, anything can happen.
    even using a struct it doesnt work.
    What do youy mean by 'it doesnt work' ? Don't forget that we are not by you. We can't guess what happens on your screen. Tell us about the details if you want help (as requested before).
    what is >>Hiding pointers<< ?
    Using a syntax that makes the pointers hidden like
    Code:
    typedef tKoerper *pKoerper;
    
    typedef struct objekt {
      pKoerper koerper;
    }tObjekt;
    I prefer
    Code:
    typedef struct objekt {
      tKoerper *p_koerper;
    }tObjekt;
    It appears clearly (including among the code) that 'p_koerper' is a pointer. Pointers are the heart of C-programming. There is no reason to hide them.
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hiding pointers: The stupidly pointless practice of typedef-ing items to be a pointer type instead of just using an * when you want a pointer. Example:
    Code:
    typedef int * Stupid;
    Stupid var; /* this is a pointer to an integer, pretty Stupid, huh? */
    int *ptr; /* Much better! */
    [edit] Curses, foiled again! [/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM