![]() |
| | #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;
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. |
| transgalactic2 is offline | |
| | #2 |
| Wheres the lesbians? Join Date: Oct 2006 Location: UK
Posts: 1,219
| The typedef allows you to refer to your struct later on as: Code: object something; Code: struct object something;
__________________ Senior highbrow doctor of authority. |
| mike_g is offline | |
| | #3 |
| Banned Join Date: Oct 2008
Posts: 1,535
| thats why they should write Code: typedef struct object object; it makes no sense using it in the building of a data type and there is no two names after the struct ?? |
| transgalactic2 is offline | |
| | #4 |
| Wheres the lesbians? Join Date: Oct 2006 Location: UK
Posts: 1,219
| eh ???
__________________ Senior highbrow doctor of authority. |
| mike_g is offline | |
| | #5 |
| Banned Join Date: Oct 2008
Posts: 1,535
| in order that we could do Code: object something; 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;
Code: typedef struct object{
int data;
struct object *left;
struct object *right;
}object;
Last edited by transgalactic2; 10-26-2008 at 05:46 AM. |
| transgalactic2 is offline | |
| | #6 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| 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. |
| matsp is offline | |
| | #7 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,166
| Quote:
Code: typedef struct { int x; char *y; } odradek;
odradek actual_odek;
| |
| MK27 is offline | |
| | #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. |
| transgalactic2 is offline | |
| | #9 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| the "second_name" can (and often is) the same as "first_name". Code: typdef struct object object; -- 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. |
| matsp is offline | |
| | #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;
Last edited by transgalactic2; 10-26-2008 at 07:46 AM. |
| transgalactic2 is offline | |
| | #11 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,166
| Quote:
Code: typedef struct ?? { int x; char y; } odredek;
//don't really put ??
odradek actual_odek;
Code: typedef struct o_dek odradek;
struct o_dek { int x; char *y; };
odradek actual_odek;
| |
| MK27 is offline | |
| | #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;
Code: typedef struct name1 name2; Code: typedef struct ?? {
int x;
char y;
} odredek;
odradek actual_odek;
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. |
| transgalactic2 is offline | |
| | #13 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,166
| Quote:
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);
}
The free() call demonstrates that main() did recieve a malloc'd object. Of course, you should do that anyway. | |
| MK27 is offline | |
| | #14 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,809
| |
| tabstop is offline | |
| | #15 |
| Registered User Join Date: Oct 2001
Posts: 2,110
| You can define a struct like this: Code: struct mytype {
int elem1, elem2;
char elem3;
};
Code: struct mytype myinstance; myinstance.elem1 = 1; myinstance.elem2 = 2; Code: struct mytype {
int elem1, elem2;
char elem3;
} myinstance;
myinstance.elem1 = 1;
myinstance.elem2 = 2;
Code: struct {
int elem1, elem2;
char elem3;
} myinstance;
myinstance.elem1 = 1;
myinstance.elem2 = 2;
struct mytype anotherinstance; /* error: no type 'struct mytype' */
Code: typedef int x;
x i;
int y;
y = 5;
i = y;
printf("%d", i);
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' */
Code: typedef struct mytype {
int elem1, elem2;
char elem3;
} anothertype;
struct mytype myinstance;
anothertype anotherinstance;
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' |
| robwhit is offline | |
![]() |
| Tags |
| struct, typedef |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| adding line numbers and concatenating a filename | durrty | C Programming | 25 | 06-28-2008 03:36 AM |
| Need help understanding info in a header file | hicpics | C Programming | 8 | 12-02-2005 12:36 PM |
| Please STICKY this- vital to MSVC 6 dev - BASETSD.h | Bubba | Game Programming | 11 | 03-15-2005 09:22 AM |
| Trouble replacing line of file | Rpog | C Programming | 4 | 04-19-2004 10:22 AM |
| Contest Results - May 27, 2002 | ygfperson | A Brief History of Cprogramming.com | 18 | 06-18-2002 01:27 PM |