what is the meaing of this typedef line..

This is a discussion on what is the meaing of this typedef line.. within the C Programming forums, part of the General Programming Boards category; Code: typedef struct object{ int data; struct object *left; struct object *right; }object; this is a code for the root ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    what is the meaing of this typedef line..

    Code:
    typedef struct object{
      int data;
      struct object *left;
     struct object  *right;
    }object;
    this is a code for the root of a tree
    why do they add typedef
    it makes no sense using it in the building of a data type

    and there is no two names after the struct
    Last edited by transgalactic2; 10-26-2008 at 03:58 AM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    The typedef allows you to refer to your struct later on as:
    Code:
    object something;
    Otherwise you would have to use:
    Code:
    struct object something;
    everytime you want to declare an object.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thats why they should write
    Code:
    typedef struct  object  object;
    but here typedef used in a very odd way

    it makes no sense using it in the building of a data type

    and there is no two names after the struct
    ??

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    eh ???

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in order that we could do
    Code:
    object something;
    like you said

    we need to write the code in this way
    Code:
    typedef struct object object; 
     struct object{
      int data;
      struct object *left;
     struct object  *right;
    }object;
    not like they did
    Code:
    typedef struct object{
      int data;
      struct object *left;
     struct object  *right;
    }object;
    why is it ok to do it in the way they write it??
    Last edited by transgalactic2; 10-26-2008 at 05:46 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because the second form is entirely identical, but one line shorter? There is technically no difference between those two forms. It is however very much the convention to use one typedef/struct definition, rather than doing one typedef and one struct definition.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    in order that we could do
    Code:
    object something;
    like you said

    we need to write the code in this way
    In fact, you can do this:

    Code:
    typedef struct { int x; char *y; } odradek; 
    
    odradek actual_odek;
    The datatype syntax is not half so dangerous as the poiner syntax
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in the simple form of typedef we use two names.
    Code:
    typedef struct first_name second_name;

    i cant see those names in this code

    Code:
    typedef struct object{
      int data;
      struct object *left;
     struct object  *right;
    }object;
    Last edited by transgalactic2; 10-26-2008 at 07:14 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    the "second_name" can (and often is) the same as "first_name".

    Code:
    typdef struct object object;
    is perfectly valid. There is no difference between that and defining the contents of the struct inside the typedef.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in this code
    Code:
    typedef struct object{
      int data;
      struct object *left;
     struct object  *right;
    }object;
    where are the locations of the first and the second names in this code?
    Last edited by transgalactic2; 10-26-2008 at 07:46 AM.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    where are the locations of the first and the second names in this code?
    There is such a thing as an "anonymous" struct. It doesn't have a name. When I wrote you can do this:

    Code:
    typedef struct ?? { int x; char y; } odredek;
    //don't really put ??
    odradek actual_odek;
    It could have been:
    Code:
    typedef struct o_dek odradek;
    struct o_dek { int x; char *y; };
    odradek actual_odek;
    If you want to be able to use the two names, but usually there is no purpose to that, so you can use the first method, where the struct is anonymous.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant understand this code:


    we create a data type using struct
    Code:
    struct object{
      int data;
      struct object *left;
     struct object  *right;
    }object;
    we can create an alias for struct name1 using
    Code:
    typedef struct name1 name2;
    but i dont know what the meaning of adding typedef to the code
    Code:
    typedef struct ?? {
     int x;
     char y; 
    } odredek;
    
    odradek actual_odek;
    first you create some variable called odredek with type "??"(some_name)

    but i cant understand why to add the word typedef because i cant see here the struct name1=name2 replacement issue

    what its affect?
    Last edited by transgalactic2; 10-26-2008 at 09:23 AM.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    first you create some variable called odredek with type "??"(some_name)

    but i cant understand why to add the word typedef because i cant see here the struct name1=name2 replacement issue

    what its affect?
    The "??" was just to indicate that you're right, there could be an object name there, but there does not have to be, which means the struct is anonymous. The only object name we need is the name of our new datatype (odradek), created with the command typedef. The base type of odradek was/is an anonymous struct.

    Yes, you could have done much the same thing by just creating a struct, odradek. One advantage to creating a new datatype with typedef is that you can now have a function return it:

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct { int x; char *y; } odradek;
    
    odradek Example (int x, char *y) {
    	odradek a_odek;
    	a_odek.x=x;
    	a_odek.y=malloc(strlen(y)+1);
    	strcpy(a_odek.y, y);
    	return a_odek;
    }
    
    
    int main() {
    	int x=10;
    	char this[]="that would be";
    	odradek example=Example(x,this);
    	printf("%d: %s\n",example.x,example.y);
    	free(example.y); 
    }
    This works with a normal struct, too. Many, many library objects are actually structs of this sort, which I'm sure is part of the reason you can get around having to refer to "struct X" all the time.

    The free() call demonstrates that main() did recieve a malloc'd object. Of course, you should do that anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by transgalactic2 View Post
    in this code
    Code:
    typedef struct object{
      int data;
      struct object *left;
     struct object  *right;
    }object;
    where are the locations of the first and the second names in this code?
    The first is in red, the second is in green.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You can define a struct like this:
    Code:
    struct mytype {
    	int elem1, elem2;
    	char elem3;
    };
    You can then define an instance of 'struct mytype' like this:
    Code:
    struct mytype myinstance;
    
    myinstance.elem1 = 1;
    myinstance.elem2 = 2;
    You can do both of those things at once like this:
    Code:
    struct mytype {
    	int elem1, elem2;
    	char elem3;
    } myinstance;
    
    myinstance.elem1 = 1;
    myinstance.elem2 = 2;
    You can make an instance of that struct without giving it's type a name. That's called an 'anonymous struct'.
    Code:
    struct {
    	int elem1, elem2;
    	char elem3;
    } myinstance;
    
    myinstance.elem1 = 1;
    myinstance.elem2 = 2;
    
    struct mytype anotherinstance; /* error: no type 'struct mytype' */
    typedef creates an alias of a type
    Code:
    typedef int x;
    
    x i;
    int y;
    
    y = 5;
    i = y;
    printf("&#37;d", i);
    You can use typedef to create aliases of structs
    Code:
    struct mytype {
    	int elem1, elem2;
    	char elem3;
    };
    
    typedef struct mytype anothertype;
    
    struct mytype myinstance; /* 'struct' keyword needed */
    anothertype anotherinstance; /* 'struct' keyword not needed */
    void f(struct mytype v1, anothertype v2);
    
    f(myinstance, anotherinstance);
    f(anotherinstance, myinstance);
    
    struct anothertype evenanotherinstance; /* error: no type 'struct anothertype' */
    You can declare the typedef and define the struct type in the same statement
    Code:
    typedef struct mytype {
    	int elem1, elem2;
    	char elem3;
    } anothertype;
    
    struct mytype myinstance;
    anothertype anotherinstance;
    This is equivalent to above. Note that in the context of a typedef statement, an identifier between the closing brace and the semicolon is the name of the new type, not the name of a struct instance.

    You can make a typedef to a struct type that doesn't have a name
    Code:
    typedef struct {
    	int elem1, elem2;
    	char elem3;
    } anothertype;
    
    anothertype anotherinstance;
    struct mytype myinstance; /* error: no type 'struct mytype' */
    Last edited by robwhit; 10-26-2008 at 01:41 PM. Reason: clarification: 'You can make a typedef to a struct type that doesn't have a name'

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

Similar Threads

  1. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  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. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread


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