C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-26-2008, 03:53 AM   #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.
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 04:48 AM   #2
Wheres the lesbians?
 
mike_g's Avatar
 
Join Date: Oct 2006
Location: UK
Posts: 1,219
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.
__________________
Senior highbrow doctor of authority.
mike_g is offline   Reply With Quote
Old 10-26-2008, 04:52 AM   #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
??
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 05:01 AM   #4
Wheres the lesbians?
 
mike_g's Avatar
 
Join Date: Oct 2006
Location: UK
Posts: 1,219
eh ???
__________________
Senior highbrow doctor of authority.
mike_g is offline   Reply With Quote
Old 10-26-2008, 05:43 AM   #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.
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 05:58 AM   #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   Reply With Quote
Old 10-26-2008, 06:58 AM   #7
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,166
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
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 10-26-2008, 07:11 AM   #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   Reply With Quote
Old 10-26-2008, 07:32 AM   #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;
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.
matsp is offline   Reply With Quote
Old 10-26-2008, 07:43 AM   #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.
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 08:41 AM   #11
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,166
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 10-26-2008, 09:19 AM   #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.
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 10:05 AM   #13
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,166
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 10-26-2008, 10:58 AM   #14
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,809
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.
tabstop is offline   Reply With Quote
Old 10-26-2008, 11:00 AM   #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;
};
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("%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'
robwhit is offline   Reply With Quote
Reply

Tags
struct, typedef

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:12 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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