question about typedef

This is a discussion on question about typedef within the C++ Programming forums, part of the General Programming Boards category; Is anyone here familiar with using typedef like this: Code: typedef string type[]; What exactly does that do?...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    question about typedef

    Is anyone here familiar with using typedef like this:

    Code:
    typedef string type[];
    What exactly does that do?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    A typedef declaration does not introduce a new type,only a synonym for the type specified.
    In your example
    Code:
    typedef string type[];
    ...
    type strArray = { "Hello","World"};

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640

    Re: question about typedef

    Originally posted by volk
    What exactly does that do?
    yeah, like Wledge said. it makes 'type' represent a string array. now you dont need all that harsh typing of "string[]" you can just use "type".

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Why wouldn't that typedef be written like this instead:

    typedef string[] type;

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by 7stud
    Why wouldn't that typedef be written like this instead:

    typedef string[] type;
    im not sure if this is true for c++ (ive been programming alot of Java at work lately) but i dont think it matters where the brackets go.

    string [] var; == string var[];

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I'm pretty sure that only:

    type name[];

    is valid in C++.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I thought with a typedef what's on the right is a synonym for what's on the left. So, it seems to me if you declared your typedef like this:

    typedef string type[]

    then type[] would be the synonym for string, and you would have to declare a string like this:

    type[] text;

    What's the deal with typedef?

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    this is the way it's used:
    Code:
    ...
    typedef char string20[20];
    string20 name;
    cin.getline(name,20,'\n');
    ...
    this is C++ - i'm not sure about java...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Originally posted by 7stud
    I thought with a typedef what's on the right is a synonym for what's on the left. So, it seems to me if you declared your typedef like this:

    typedef string type[]

    then type[] would be the synonym for string, and you would have to declare a string like this:

    type[] text;

    What's the deal with typedef?
    Not necessarily, exactly. Its syntax is supposed to resemble that of a variable declaration. Think of these:
    Code:
    char foo[5];
    typedef char bar[] // the example in question
    foo's type is a char[], just like "bar" is a synonym for char[]. So the example works because it's analogous to the way you declare an array of something.

    I think it *does* matter where the brackets go:
    Code:
    char* foo;    // Either the * in front
    char bar[5]; // or brackets after
    
    typedef char* foo; // ditto
    typedef char foo[];
    But I have not yet been able to check these.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with ZwQueryDirectoryFile for my Driver.
    By taDo in forum Windows Programming
    Replies: 9
    Last Post: 11-27-2008, 07:54 AM
  2. Error: redefinition of typedef xxxx
    By Rocha in forum C Programming
    Replies: 2
    Last Post: 11-24-2008, 08:19 AM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Help...typedef struct...confusing...
    By darkchild in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 07:03 AM
  5. Simple typedef question
    By Nutshell in forum C Programming
    Replies: 17
    Last Post: 06-27-2002, 02:56 PM

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