C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-26-2008, 11:06 AM   #16
Banned
 
Join Date: Oct 2008
Posts: 1,535
i understood your anonymous struct
creating a data type from an anonymous name

but it not the case here:



Code:
typedef struct object{
  int data;
  struct object *left;
 struct object  *right;
}object;
i have a variable called "object"
but there is no anonimus struct
on the contrary i have the first name but not the alias second name
??
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 11:12 AM   #17
Banned
 
Join Date: Oct 2008
Posts: 1,535
so the "object" in the last line unlike normal structs does not represent a variable but a variable type
an alias to
struct object

in a regular struct like this:
Code:
struct mytype {
	int elem1, elem2;
	char elem3;
} object;

object.elem1 = 1;
object.elem2 = 2;       //object represents a variable here unlike my example

Last edited by transgalactic2; 10-26-2008 at 11:17 AM.
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 11:15 AM   #18
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by transgalactic2 View Post
so the "object" in the last line unlike normal structs does not represent a variable but an alias to
struct object
Well, that's what a typedef does. Makes the second thing an alias for the first thing.
tabstop is offline   Reply With Quote
Old 10-26-2008, 11:25 AM   #19
Banned
 
Join Date: Oct 2008
Posts: 1,535
regarding this code:
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); 
}
i know that variable "this" is of type orbarek
but in order to assign values to "this" (in java i used a costructor)
where is it in this code?

Last edited by transgalactic2; 10-26-2008 at 11:29 AM.
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 11:30 AM   #20
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
this is an array of characters while a_odek is of the type odradek which is an alias of the complex type you have defined.
itCbitC is offline   Reply With Quote
Old 10-26-2008, 11:32 AM   #21
Banned
 
Join Date: Oct 2008
Posts: 1,535
aahhh sorryy i thought there is "this" and constructors in C
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 11:32 AM   #22
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
The variable "this" is of type char[], and it is initialized (remember initialization?) right where it is declared, one line above the line you highlighted. To change its value, you should use strcpy, just as you did in the Example function.
tabstop is offline   Reply With Quote
Old 10-26-2008, 11:37 AM   #23
Banned
 
Join Date: Oct 2008
Posts: 1,535
thanks
transgalactic2 is offline   Reply With Quote
Old 10-26-2008, 11:49 AM   #24
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> i have a variable called "object"
You have two things, and neither of them are objects.

Code:
typedef struct object{
  int data;
  struct object *left;
 struct object  *right;
}object;
The names in red are the structure tag name, which serves to identify the struct by name. You NEED a tag name if you're creating a self-referencing structure like you're doing here.
Much of the time, the tag name is omitted if the struct is embedded in a typedef declaration (such as this).

The blue name is the name of the typedef name for the struct.
It's a completely different name to the red one.

BOTH can be used to create objects, like so.
Code:
object foo;
struct object bar;
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 10-26-2008, 12:21 PM   #25
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by transgalactic2 View Post
i understood your anonymous struct
creating a data type from an anonymous name

but it not the case here:



Code:
typedef struct object{
  int data;
  struct object *left;
 struct object  *right;
}object;
i have a variable called "object"
but there is no anonimus struct
on the contrary i have the first name but not the alias second name
??
You seem to misunderstand.
The "object" part at the end is part of the typedef, and does not create an instance of the struct.
The actual definition or declaration of the struct is ignored, so the above code would ultimately look like:
Code:
typedef struct object object;
(Note that I just cut out the contents of the struct type.)
If, on the other hand, we don't put typedef before the struct, so it becomes:
Code:
struct object{
  int data;
  struct object *left;
 struct object  *right;
}object;
Then it does indeed create an instance of the struct called object.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia 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 04:01 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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