Help understanding typedef structs

This is a discussion on Help understanding typedef structs within the C Programming forums, part of the General Programming Boards category; I understand basic structures with typedef like the following: Code: typedef struct { int month, day, year; } date; date ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Help understanding typedef structs

    I understand basic structures with typedef like the following:
    Code:
    typedef struct {
       int month, day, year;
    } date;
    date var1, var2;
    But I just can't figure out what something like this for a stack does:
    Code:
    typedef struct {
       int stack[min];
       int x;
    } stack, *stackPtr;
    
    void createStack (stackPtr s); //prototype
    What is the purpose of *stackPtr? If I wanted to modify the structure within a function, can't I just use *stack?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    It is just like doing two separate typedef statements. It would be the equivalent to:

    Code:
    typedef struct {
       int stack[min];
       int x;
    } stack;
    typedef stack *stackptr;
    This way if you need to declare a pointer to a stack, you can do
    stackptr pStack;
    or you can do it like
    stack *pStack;
    both statements do the same thing.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    This is what I have so far for my stack implementation:

    Code:
    #define MAX 10
    typedef struct {
       int stack[MAX];
       int sp;
    } stack, *stackPtr;
    
    void initStack (stackPtr s);
    
    int main()
    {
    
       //initStack(stack);
    
       return 0;
    }
    
    void initStack (stackPtr s)
    {
       s->sp = 0;
    }
    How do I call initStack from the main function? And what am I doing wrong in initStack that's causing it to crash?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You're never creating an instance of the struct; you're only defining it.

    Change main() to this:
    Code:
    int main(void)
    {
      stack henry;
    
      initStack(&henry);
    
      return 0;
    }
    Of course, you don't have to call your stack henry
    If you understand what you're doing, you're not learning anything.

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, 11:36 AM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 08:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06: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, 11:54 AM

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