regarding pointers to characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam_cit@yahoo.co.in

    regarding pointers to characters

    Hi Everyone,

    I have the following two cases,

    typedef char* cptr;
    const cptr ch;
    //here ch is a pointer to a character and the pointer is a constant
    here...

    where as

    #define CPTR char*
    const CPTR ch;
    //here ch is a pointer to character and the character is a constant
    here...

    What is the reason behind this???

  • Keith Thompson

    #2
    Re: regarding pointers to characters

    sam_cit@yahoo.c o.in writes:
    Hi Everyone,
    >
    I have the following two cases,
    >
    typedef char* cptr;
    const cptr ch;
    //here ch is a pointer to a character and the pointer is a constant
    here...
    >
    where as
    >
    #define CPTR char*
    const CPTR ch;
    //here ch is a pointer to character and the character is a constant
    here...
    >
    What is the reason behind this???
    Is this a homework problem?

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • sam_cit@yahoo.co.in

      #3
      Re: regarding pointers to characters

      >
      Is this a homework problem?
      >
      No, not at all, homeworks were a long time ago, i know that macro
      (string replacement) is before compilation, but i'm not very clear with
      typedefs, i thought answer to this might help me understand it better...

      Comment

      • Keith Thompson

        #4
        Re: regarding pointers to characters

        sam_cit@yahoo.c o.in writes:
        I have the following two cases,
        >
        typedef char* cptr;
        const cptr ch;
        //here ch is a pointer to a character and the pointer is a constant
        here...
        >
        where as
        >
        #define CPTR char*
        const CPTR ch;
        //here ch is a pointer to character and the character is a constant
        here...
        >
        What is the reason behind this???
        A typedef creates an alias for a type. Macro expansion works on text
        (tokens, actually) with no regard for what those tokens might mean in
        context.

        In your second example, "CPTR" expands to "char*", so the declaration
        is:

        const char* ch;

        Consider also the following example:

        #include <stdio.h>

        #define SIX 1+5
        #define NINE 8+1

        int main(void)
        {
        printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
        return 0;
        }

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        Working...