#define a char pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mailursubbu@gmail.com

    #define a char pointer

    Hi,

    Will it be possible to #define a char pointer...
    It means if write some thing like

    #define CHAR_PTR (char *) // I know this wont work


    I should be able to use CHAR_PTR to define a variable of type char *.

    Best Regards,
    Subramanya

  • Frederick Gotham

    #2
    Re: #define a char pointer

    posted:
    Hi,
    >
    Will it be possible to #define a char pointer...
    It means if write some thing like
    >
    #define CHAR_PTR (char *) // I know this wont work
    >
    >
    I should be able to use CHAR_PTR to define a variable of type char *.

    (1) The disgusting option:

    #define CHAR_PTR char*


    This will break if you use multiple definitions:

    CHAR_PTR p1,p2,p3,p4;

    ~

    (2) The beautiful option:

    typedef char *charptr;



    --

    Frederick Gotham

    Comment

    • Eric Sosman

      #3
      Re: #define a char pointer

      mailursubbu@gma il.com wrote:
      Hi,
      >
      Will it be possible to #define a char pointer...
      It means if write some thing like
      >
      #define CHAR_PTR (char *) // I know this wont work
      >
      >
      I should be able to use CHAR_PTR to define a variable of type char *.
      This will work all right for

      CHAR_PTR p;
      CHAR_PTR q;

      .... but will not operate as intended for

      CHAR_PTR p, q;
      /* equivalent to
      char *p, q;
      equivalent to
      char *p; -- a pointer
      char q; -- not a pointer
      */

      If you *must* do this, use a typedef:

      typedef char *CHAR_PTR;
      CHAR_PTR p, q; /* both are pointers */

      .... but most people find that using typedefs for "simple"
      pointers makes the code harder to read without improving
      its flexibility.

      If you want to "opacify" the data type, consider hiding
      the pointed-at thing rather than the pointer to it:

      typedef char CHAR_TYPE;
      CHAR_TYPE *p, *q;

      --
      Eric Sosman
      esosman@acm-dot-org.invalid

      Comment

      • Tom St Denis

        #4
        Re: #define a char pointer

        Frederick Gotham wrote:
        (2) The beautiful option:
        >
        typedef char *charptr;
        Just an aside... I like how you've progressed in this group. Good
        answer and please keep up the helpful replies.

        Tom

        [No, I'm not being sarcastic. It's rare and nice to see people join a
        group and become contributors in short order.]

        Comment

        • Frederick Gotham

          #5
          Re: #define a char pointer

          Tom St Denis posted:
          Just an aside... I like how you've progressed in this group. Good
          answer and please keep up the helpful replies.
          >
          Tom
          >
          [No, I'm not being sarcastic. It's rare and nice to see people join a
          group and become contributors in short order.]

          Thanks. I wish I could contribute more, but my knowledge of the C Standard is
          a bit shotty at the moment. Over on comp.lang.c++, I can give more
          authoritative answers, quoting from the C++ Standard and so forth; maybe I'll
          become familiar with the C Standard in time (although it's a little wishy-
          washy given the C89 Vs C99 situation).

          --

          Frederick Gotham

          Comment

          • Al Balmer

            #6
            Re: #define a char pointer

            On Mon, 24 Jul 2006 14:43:22 GMT, Frederick Gotham
            <fgothamNO@SPAM .comwrote:
            posted:
            >
            >Hi,
            >>
            > Will it be possible to #define a char pointer...
            > It means if write some thing like
            >>
            > #define CHAR_PTR (char *) // I know this wont work
            >>
            >>
            > I should be able to use CHAR_PTR to define a variable of type char *.
            >
            >
            >(1) The disgusting option:
            >
            #define CHAR_PTR char*
            >
            >
            >This will break if you use multiple definitions:
            >
            CHAR_PTR p1,p2,p3,p4;
            >
            ~
            >
            >(2) The beautiful option:
            >
            typedef char *charptr;
            Much better, but even better to just use char * :-)

            To the OP: Why?

            --
            Al Balmer
            Sun City, AZ

            Comment

            • Bart Rider

              #7
              Re: #define a char pointer

              Eric Sosman wrote:
              mailursubbu@gma il.com wrote:
              >
              >Hi,
              >>
              > Will it be possible to #define a char pointer...
              > It means if write some thing like
              >>
              > #define CHAR_PTR (char *) // I know this wont work
              >>
              >>
              > I should be able to use CHAR_PTR to define a variable of type char *.
              >
              >
              This will work all right for
              >
              CHAR_PTR p;
              CHAR_PTR q;
              >
              ... but will not operate as intended for
              >
              CHAR_PTR p, q;
              /* equivalent to
              char *p, q;
              equivalent to
              char *p; -- a pointer
              char q; -- not a pointer
              */
              >
              [...skip...]
              >
              Hmm, the original #define of the OP was:
              #define CHAR_PTR (char *)
              Now I use this in
              CHAR_PTR p, q;
              This is equivalent to
              (char *) p, q;

              Do the parenthesis anything to the type definition?
              Or is it compiles as you said (p a pointer to char, q
              a char)?

              Bart

              Comment

              • Eric Sosman

                #8
                Re: #define a char pointer

                Bart Rider wrote:
                Eric Sosman wrote:
                >
                >mailursubbu@gma il.com wrote:
                >>
                >>Hi,
                >>>
                >> Will it be possible to #define a char pointer...
                >> It means if write some thing like
                >>>
                >> #define CHAR_PTR (char *) // I know this wont work
                >>>
                >>>
                >> I should be able to use CHAR_PTR to define a variable of type char *.
                >>
                >>
                >>
                > This will work all right for
                >>
                > CHAR_PTR p;
                > CHAR_PTR q;
                >>
                >... but will not operate as intended for
                >>
                > CHAR_PTR p, q;
                > /* equivalent to
                > char *p, q;
                > equivalent to
                > char *p; -- a pointer
                > char q; -- not a pointer
                > */
                >>
                >[...skip...]
                >>
                >
                Hmm, the original #define of the OP was:
                #define CHAR_PTR (char *)
                Now I use this in
                CHAR_PTR p, q;
                This is equivalent to
                (char *) p, q;
                >
                Do the parenthesis anything to the type definition?
                Or is it compiles as you said (p a pointer to char, q
                a char)?
                You're overlooking the "as if" rule, which in this case
                is concisely stated "Eric writes `as if' he were right, even
                when he's wrong."

                In other words: Ooops! You're right, the parentheses are
                erroneous. I must have mentally deleted them to make the macro
                fit the O.P.'s stated purpose, and then wrote about the still-
                in-my-feeble-brain corrected version instead of about the
                original. Thanks for pointing out my error.

                --
                Eric Sosman
                esosman@acm-dot-org.invalid

                Comment

                • Frederick Gotham

                  #9
                  Re: #define a char pointer

                  Bart Rider posted:
                  Hmm, the original #define of the OP was:
                  #define CHAR_PTR (char *)
                  Now I use this in
                  CHAR_PTR p, q;
                  This is equivalent to
                  (char *) p, q;
                  >
                  Do the parenthesis anything to the type definition?
                  Or is it compiles as you said (p a pointer to char, q
                  a char)?

                  You learn much more by testing these things yourself.

                  Check what type-mismatch errors you get:

                  (char*) a, b;

                  a = 5;
                  b = 4;

                  --

                  Frederick Gotham

                  Comment

                  Working...