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; i understood your anonymous struct creating a data type from an anonymous name but it not the case here: Code: ...

  1. #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
    ??

  2. #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.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  4. #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("&#37;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.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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.

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    aahhh sorryy i thought there is "this" and constructors in C

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

  9. #24
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
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