Thread: Typedef understanding

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Typedef understanding

    in my tutrial a shorthand version for structure like you could do in a program
    like ?
    Code:
    #include <stdio.h>
    typedef struct employee char address[50];
    int main(void)
    {
    struct employee emp;
    printf("Name is %s",emp.adress);
    return 0;
    }
    thanks in advanced for your help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yeah, that's a pretty horrible example. Do you have a question about why it doesn't work, or what?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    More like:
    Code:
    #include <stdio.h>
    
    typedef struct
    {
        char address[50];
    } employee;
    
    int main(void)
    {
        employee emp;
    
        /* Initialise emp */
    
        printf("Name is &#37;s", emp.address);
        return 0;
    }
    Of course, the struct could also be named.
    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

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yes thanks laserswitch i understand it now its just
    a shortcut in way to not like struct employee variable you just do
    typedef struct employee thenyourshortcut.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i tried this code
    Code:
    #include <stdio.h>
    struct employee
    {
    char name[50];
    char address[51];
    int age;
    float salary;
    };
    typedef struct employee shortcut;
    int main(void)
    {
    shortcut x;
    fputs("Please Enter your age: ",stdout);
    scanf("&#37;d",x.age);
    printf("Age is %d",x.age);
    fputs("Please Enter your salary: ",stdout);
    scanf("%f",x.salary);
    printf("Age is %f",x.salary);
    return 0;
    }
    but program crashed i dunt know why though.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    nvm lol i was too fast i forgot refressing it though lol & behind scanf variable
    Code:
     
    #include <stdio.h>
    struct employee
    {
    char name[50];
    char address[51];
    int age;
    float salary;
    };
    typedef struct employee shortcut;
    int main(void)
    {
    shortcut x;
    fputs("Please Enter your age: ",stdout);
    scanf("&#37;d",&x.age);
    printf("Age is %d",x.age);
    fputs("Please Enter your salary: ",stdout);
    scanf("%f",&x.salary);
    printf("Age is %f",x.salary);
    return 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Indentation - learn how to do it please!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM