![]() |
| | #16 |
| Banned Join Date: Oct 2008
Posts: 1,535
| 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;
but there is no anonimus struct on the contrary i have the first name but not the alias second name ?? |
| transgalactic2 is offline | |
| | #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 | |
| | #18 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #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);
}
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 | |
| | #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 | |
| | #21 |
| Banned Join Date: Oct 2008
Posts: 1,535
| aahhh sorryy i thought there is "this" and constructors in C |
| transgalactic2 is offline | |
| | #22 |
| and the Hat of Guessing 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 | |
| | #23 |
| Banned Join Date: Oct 2008
Posts: 1,535
| thanks |
| transgalactic2 is offline | |
| | #24 |
| and the hat of vanishing 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;
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 | |
| | #25 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
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; 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;
__________________ 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:
| ||
| Elysia 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 |