Like Tree1Likes

Typedef

This is a discussion on Typedef within the C Programming forums, part of the General Programming Boards category; just a question Code: struct stackNode { int data; struct stackNode *nxt } typedef struct stackNode StackNode; typedef StackNode *StackNodePtr; ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    66

    Typedef

    just a question
    Code:
    struct stackNode
    {
    int data;
    struct stackNode *nxt
    }
    typedef struct stackNode StackNode;
    typedef StackNode *StackNodePtr;
    is
    Code:
    typedef struct stackNode StackNode;
    the same as
    Code:
    struct stackNode StackNode
    and is
    Code:
    typedef StackNode *StackNodePtr;
    the same as
    Code:
    struct stackNode *StackNodePtr
    can anyone transcribe ? this or ? correct me? I am kinda getting confused

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kyel_dai92 View Post
    just a question
    Code:
    struct stackNode
    {
    int data;
    struct stackNode *nxt
    }
    typedef struct stackNode StackNode;
    typedef StackNode *StackNodePtr;
    is
    Code:
    typedef struct stackNode StackNode;
    the same as
    Code:
    struct stackNode StackNode
    Not exactly....

    struct stackNode StackNode defines one structure that can be used in your program.
    But typedef struct stackNode Stacknode defines a new type that can be used to instantiate structs as in...
    Code:
    stackNode Fred;
    where Fred is now of type stackNode and can access the variables inside the struct.

    Code:
    and is 
    Code:
    typedef StackNode *StackNodePtr;
    the same as
    Code:
    struct stackNode *StackNodePtr
    It's the same concept as above except this time you're defining pointers to stackNodes.

    can anyone transcribe ? this or ? correct me? I am kinda getting confused
    It is kind of confusing till you use it a few times...

    Just to make it worse, there is a complete alternative method, used extensively in windows, that I like to use...
    Code:
    typedef struct tagStackNode     
      { int data;
         tagStackNode *next; }
       StackNode, *pStackNode;
    This describes the struct, a pointer to same type inside the struct, and two new types ...
    StackNode is used to instantiate a struct as in
    Code:
    StackNode Fred;
    pStackNode is used to create a pointer to a struct as in
    Code:
    pStackNode  John;
    
    John = malloc(sizeof(StackNode));
    These are all ways of doing the same thing... creating structures and creating pointers to structures. Since the form of your struct definition appears to be for a linked list (and there are many many other uses) I would find the last form most convenient.
    Last edited by CommonTater; 04-05-2011 at 09:12 AM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Don't use devil-words like "instantiate" in C forum.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Don't use devil-words like "instantiate" in C forum.
    Oh my ....

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,514

    Smile

    Quote Originally Posted by nonoob View Post
    Don't use devil-words like "instantiate" in C forum.
    I object; CommonTater showed some class when he used the word "instantiate".

    Tim S.

    Warning: Incorrigible punster don't encourage.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It's a struct without method!

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    It's a struct without method!
    Yet strangely, it does exist.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    I think Tater covered it pretty well, but due to your confusing code, he made a few small boo boos:

    Quote Originally Posted by CommonTater View Post
    Not exactly....
    struct stackNode StackNode defines one structure that can be used in your program.
    But typedef struct stackNode Stacknode defines a new type that can be used to instantiate structs as in...
    Code:
    stackNode Fred;
    This should actually be StackNode Fred. stackNode is the struct name, which would require the word struct in front of it before the variable definition. StackNode, with the capital S is shorthand for struct stackNode.

    Code:
    and is 
    Code:
    typedef StackNode *StackNodePtr;
    the same as
    Code:
    struct stackNode *StackNodePtr
    It's the same concept as above except this time you're defining pointers to stackNodes.
    One is a type definition and the other is a variable definition. The former defines a new type, StackNodePtr, that is a pointer to type StackNode. The latter defines a variable named StackNodePtr that is of type pointer to struct stackNode.

    Quote Originally Posted by kyel_dai92 View Post
    can anyone transcribe ? this or ? correct me? I am kinda getting confused
    So am I. A couple rules I like to follow for working with structs and typedefs, and code in general, are this:

    First off, don't make two names (type names, variable names, etc) differ only by case. That is, stackNode and StackNode are very confusing and lead to all kinds of problems, from a simple miss of the shift key. I often use a simple suffix:
    Code:
    typedef struct stack_s      stack_t; // define stack_t type as shorthand for "struct stack_s"
    struct stack_s {
        int data;
        stack_t *next;  // pointer to another struct, just like this one
    };
    
    stack_t *top = NULL;  // pointer to the top of the stack
    I use the _s for structs and _t for typedef'ed types. I also like putting the typedef above the struct definition, since it allows me to use the shorthand name inside the struct.

    Second, don't mask indirection/pointerness in a typedef. It makes it difficult to track down problems and really know what your code is doing with memory. Avoid anything like:
    Code:
    typedef StackNode *StackNodePtr;  // evil, don't use a * in a typedef
    rags_to_riches likes this.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    OMG I am confuse , soo they are not the same? well the they have the same concept but , anyways I'll put my whole coede here I guess? just to make it more understandable


    This is the original code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct stackNode
    {
           int data;
           struct stackNode *nxt;
           };
    
    typedef struct stackNode StackNode;
    typedef StackNode *StackNodePtr;
    
    
    
    
    void instructions()
    {
         printf("        Please Select a Number to do the following\n\n");
         printf("              [1]Push a value\n");
         printf("              [2]Pop a value\n");
         printf("              [2]Peek a node\n");
         printf("              [3]Display\n");
         printf("              [4]Exit\n               ");
         }
         
         int main ()
         {
             int value; 
             int choice;
             Stack = NULL;
             
             instructions();
         
         do
         {
           scanf("%d",&choice);
             system("cls");
             
             
             if(choice == 1)
                 {
                     
                                           }
                         
                  if(choice == 2)
                  {
                            printf(" "); 
                            }       
                  if(choice == 3)
                  {
                            
                            printf(" ");
                            }
                    if(choice == 4 )
                    {
                              
                              printf("bye!");
                              return 0;
                              }      
                              
                              
                   
             }while(choice !=4);
                 
             getch();
             }

    but I changed it to

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct stackNode
    {
           int data;
           struct stackNode *nxt;
           };
    
    struct stackNode STACK;
    struct stackNode *Stack;
    
    
    
    void instructions()
    {
         printf("        Please Select a Number to do the following\n\n");
         printf("              [1]Push a value\n");
         printf("              [2]Pop a value\n");
         printf("              [2]Peek a node\n");
         printf("              [3]Display\n");
         printf("              [4]Exit\n               ");
         }
         
         int main ()
         {
             int value; 
             int choice;
             Stack = NULL;
             
             instructions();
         
         do
         {
           scanf("%d",&choice);
             system("cls");
             
             
             if(choice == 1)
                 {
                     
                                           }
                         
                  if(choice == 2)
                  {
                            printf(" "); 
                            }       
                  if(choice == 3)
                  {
                            
                            printf(" ");
                            }
                    if(choice == 4 )
                    {
                              
                              printf("bye!");
                              return 0;
                              }      
                              
                              
                   
             }while(choice !=4);
                 
             getch();
             }
    are they still the same or different?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    No, they are not the same.

    In your first piece of code, you use typedef. Using typedef does not actually create a variable. It is just a way to make a synonym for another type.
    typedef struct stackNode StackNode;
    typedef is just a keyword, that tells the compiler to make a synonym
    struct stackNode is the original type
    StackNode is the name of the new synonym. Anywhere you use struct stackNode, you can now use .StackNode

    In your second piece of code, you don't use typedef. That means you define variables. You define a variable with type variable_name
    struct stackNode STACK;
    struct stackNode *Stack;

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
    typedef struct stackNode StackNode;
    That's declaring a new data type based off of the stackNode structure - this new data type has one integer, and one pointer.

    Code:
    struct stackNode StackNode
    That just declares a structure.

    Code:
    typedef StackNode *StackNodePtr;
    That declares a new data type from the existing StackNode data type. This data type consists of a pointer and only a pointer.

    Code:
    struct stackNode *StackNodePtr
    Not sure about this last one. I'm not too familiar with typedef, I've seen it in header files, mostly paired with structures. I'm sure someone else can provide a better answer.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    Code:
    struct stackNode *StackNodePtr
    Not sure about this last one. I'm not too familiar with typedef, I've seen it in header files, mostly paired with structures. I'm sure someone else can provide a better answer.
    That's creating a (uninitialized) pointer to the struct.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    and what's the solution for typedef StackNode *StackNodeptr?

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kyel_dai92 View Post
    and what's the solution for typedef StackNode *StackNodeptr?
    Look up the typedef keyword in your C book....

    Really, it's not a difficult concept.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    66
    wait I think I get typedef StackNode *StackNodePtr; will be struct stackNode *StackNodePtr ? am I correct?

    I've redone my code now look at the errors I really don't get it with this errors can anyone tell me ?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    struct stackNode
    {
           int data;
           struct stackNode *nextPtr;
           };
           
          typedef struct stackNode StackNode;
          typedef StackNode *StackNodePtr;
          
          
    void instructions();
    void push(StackNodePtr *, int value);
    void print( StackNodePtr );
                
                
    int main()
    {
        StackNode stackPtr = NULL;
        int choice, value;
        instructions();
        
          do
         {
           scanf("%d",&choice);
             system("cls");
             
             
             if(choice == 1)
                 {
                     printf("Enter  a value for the stac");
                     scanf("%d",&value);
                     push(&stackPtr,value);
                     printf( stackPtr );
                                           }
                         
                  if(choice == 2)
                  {
                            printf(" "); 
                            }       
                  if(choice == 3)
                  {
                            
                            printf(" ");
                            }
                    if(choice == 4 )
                    {
                              
                              printf("bye!");
                              return 0;
                              }      
                              
                              
                   
             }while(choice !=4);
       
        
        system("pause");
        }
    
    
      void instructions()
      {
           printf("[1]Push a value on the stack\n");
           printf("[2]Pop a value off the stack\n");
           printf("[3]Display the whole stack\n");
           printf("[4]Exit");
           } 
         
         void push (StackNodePtr *topPtr, int info)
         {
              StackNodePtr newPtr;
               newPtr = (StackNodePtr)malloc(sizeof(StackNode));
              if(newPtr !=NULL)
              {
                        newPtr->data=info;
                        newPtr->nextPtr=*topPtr;
                        *topPtr = newPtr;
                        }
              }
    
    
    void print(StackNode Ptr current Ptr)
    {
         if(current == NULL)
         printf("Stack Has No Entry");
         else
         {
             printf("the stack is :\n");
             while(currentPtr !=NULL)
             {
                              printf("%d -->",currrent->data);
                              currentPtr = currentPtr->nextPtr;
                              }     
             }
          
         }
    I've redone my code now look at the errors I really don't get it with this errors can anyone tell me ?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    struct stackNode
    {
           int data;
           struct stackNode *nextPtr;
           };
           
          typedef struct stackNode StackNode;
          typedef StackNode *StackNodePtr;
          
          
    void instructions();
    void push(StackNodePtr *, int value);
    void print( StackNodePtr );
                
                
    int main()
    {
        StackNode stackPtr = NULL;
        int choice, value;
        instructions();
        
          do
         {
           scanf("%d",&choice);
             system("cls");
             
             
             if(choice == 1)
                 {
                     printf("Enter  a value for the stac");
                     scanf("%d",&value);
                     push(&stackPtr,value);
                     printf( stackPtr );
                                           }
                         
                  if(choice == 2)
                  {
                            printf(" "); 
                            }       
                  if(choice == 3)
                  {
                            
                            printf(" ");
                            }
                    if(choice == 4 )
                    {
                              
                              printf("bye!");
                              return 0;
                              }      
                              
                              
                   
             }while(choice !=4);
       
        
        system("pause");
        }
    
    
      void instructions()
      {
           printf("[1]Push a value on the stack\n");
           printf("[2]Pop a value off the stack\n");
           printf("[3]Display the whole stack\n");
           printf("[4]Exit");
           } 
         
         void push (StackNodePtr *topPtr, int info)
         {
              StackNodePtr newPtr;
               newPtr = (StackNodePtr)malloc(sizeof(StackNode));
              if(newPtr !=NULL)
              {
                        newPtr->data=info;
                        newPtr->nextPtr=*topPtr;
                        *topPtr = newPtr;
                        }
              }
    
    
    void print(StackNode Ptr current Ptr)
    {
         if(current == NULL)
         printf("Stack Has No Entry");
         else
         {
             printf("the stack is :\n");
             while(currentPtr !=NULL)
             {
                              printf("%d -->",currrent->data);
                              currentPtr = currentPtr->nextPtr;
                              }     
             }
          
         }
    Last edited by kyel_dai92; 04-06-2011 at 05:05 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. 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
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. 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
  5. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM

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