whats the meaning of typedef

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cool17
    New Member
    • Oct 2006
    • 24

    whats the meaning of typedef

    Hi
    can somebody tell me what does this line means in the c programming "typedef struct terms* express;"?

    thank
  • tyreld
    New Member
    • Sep 2006
    • 144

    #2
    The typedef keyword is used to introduce synonyms for types which are intrinsic types (int, float, etc.) or where declared some other way (structs, classes). The new name becomes equivalent to the type you wanted.

    Code:
    struct terms {
      // some fields
    };
    
    // Declare a pointer of struct terms.
    struct terms *termptr;
    Now we accomplish the same thing using a typedef.

    Code:
    struct terms {
      // some fields
    };
    
    typedef struct terms *express;
    
    // The decleration of "termptr" is equivalent to our previous example
    express termptr;
    I generally think it is a poor idea to introduce a new type name for pointer types that don't explicitly indicate in the type name that it is a pointer type. In the last example I think it would be better to have named the new type "p_express" , or "express_pt r", or "terms_ptr" , or "p_terms."

    Comment

    • cool17
      New Member
      • Oct 2006
      • 24

      #3
      okok thx

      but will it be the same as in this case the '*' is in front of the term and not the express

      typedef struct terms* express;


      so will it make any different?

      Comment

      • cool17
        New Member
        • Oct 2006
        • 24

        #4
        The code is as follow, thanks again

        #ifndef _EXPRESSION_H
        #define _EXPRESSION_H


        typedef struct terms* express;

        typedef struct expressions* myexp;

        struct expressions
        {
        express* exp;
        int numberOfTerms;
        };

        struct terms
        {

        double x_value;
        double x_power;
        double x_times;
        int positive;
        int isX;
        int KnowX;

        };



        myexp createExpressio n();

        void string2Expressi on(char*,myexp* );

        char* expression2Stri ng(myexp);


        #endif

        Comment

        • m013690
          New Member
          • Sep 2006
          • 23

          #5
          no, the whitespace does not matter with regard to the pointer declaration.

          terms *express
          is the same as
          terms* express

          Comment

          • tyreld
            New Member
            • Sep 2006
            • 144

            #6
            Originally posted by cool17
            okok thx

            but will it be the same as in this case the '*' is in front of the term and not the express

            typedef struct terms* express;


            so will it make any different?
            No, there will be no difference.

            Code:
            typedef struct terms *express;
            is the same as

            Code:
            typedef struct terms* express;
            is the same as

            Code:
            typedef struct terms * express;
            It is mostly a matter of personal preference to a certain extent. However, the first example is how most programmers and documentation will write the declaration.

            Comment

            • cool17
              New Member
              • Oct 2006
              • 24

              #7
              thank guys...

              Comment

              Working...