typedef

This is a discussion on typedef within the C Programming forums, part of the General Programming Boards category; Hi, I wonder if somebody could help me out. If I write this: char *a, b, c; only a is ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    typedef

    Hi,

    I wonder if somebody could help me out.

    If I write this:

    char *a, b, c;

    only a is a pointer to char, b and c are just variables.

    However if I do this:
    typedef char* STRING;

    and then I do this:
    STRING a, b, c;

    this is then equivalent to:
    char *a, *b, *c;

    that is three pointers to char. I tested this out to confirm this. I just can't
    understand why all three variables above end up as pointers.

    Thanks in advance for any help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,796
    Because a typedef is not dumb text substitution (unlike a macro, which isn't completely dumb, but still more like it). STRING is an alias for char*, so the variables declared are all of type char*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    You defined STRING as character pointer.

    You then defined a,b and c as type STRING. Which equals a character pointer.

    Much the same way going int a,b,c; defines a,b,c as integer variables.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    Thanks laserlight, now I understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typedef
    By kyel_dai92 in forum C Programming
    Replies: 21
    Last Post: 04-07-2011, 09:14 AM
  2. Help with typedef
    By sridharval in forum C Programming
    Replies: 2
    Last Post: 05-04-2010, 05:33 PM
  3. Typedef
    By vijay s in forum C Programming
    Replies: 2
    Last Post: 11-05-2009, 12:00 PM
  4. typedef .....
    By roaan in forum C Programming
    Replies: 9
    Last Post: 07-24-2009, 01:14 PM
  5. What does 'typedef' do?
    By supaben34 in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2002, 06:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21