structure & typedef

This is a discussion on structure & typedef within the C Programming forums, part of the General Programming Boards category; i need to discover the usage of" node ptr "-vs- "node *ptr" well.... can you find out the problem is ...

  1. #1
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    structure & typedef

    i need to discover the usage of" node ptr "-vs- "node *ptr"

    well.... can you find out the problem is ? describe more about the different? i'm still dont know the how to write the syntax and even dont know what's the usage of the 2 options.

    how about "typedef" usage?

    Code:
    --------------------------------------------------------
    void test(getnode *ptr);
    
    #include <stdio.h>
    
    typedef struct {
    	int a;
    	int b;
    } NODE;
    
    int main ()
    {
    	NODE getnode;
    	NODE *ptr;
    
    	printf ("Please enter your num > ");
    	scanf ("%d",&getnode.a);
    
    	printf ("Please enter your 2nd num > ");
    	scanf ("%d",&ptr->b);
    
    	printf ("your ans is ... %d and %d\n\n", getnode.a, ptr->b);
    
    	return 0;
    }

    Code Tags added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>NODE *ptr;
    This is a pointer to a node. Its is not a node. Therefore you cannot assign values to the node because there isn't one (yet). you need to point is at a node before you can use it.


    >>NODE getnode;
    This is a node, and you can assign to it.

    If you did this:
    >>ptr = &getnode;
    then you could assign values to ptr->a and getnode.a, but both would be updating the same struct.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    how bout "typedef" -vs- normal variable?
    eg :

    >> typedef int a;

    and

    >> int a;

    so what's the different actually?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    What you've done there is made a typed definition of an int, so this:

    a MyVar;

    is the same as:

    int MyVar;

    (Kinda pointless...)
    What you could do is:

    typedef unsigned long int ULINT

    So instead of typing unsigned long int every time you want to declare such a variable, you simply type ULINT.

    Or make a typed definition of a structure so you don't have to type struct all the time when using it:

    typedef struct
    {
    int Xpos;
    int Ypos;
    }POSITION;


    Or if you want a datatype of enumerated values:

    typedef enum
    {
    LEFT,
    RIGHT,
    UP,
    DOWN
    }DIRECTION;


    The possibilities are endless .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    you mean typedef and normal def is still the same?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    yes.......and no.

    #def int MAX 8

    is a preprocessor directive. Wherever MAX appears in the code it will be replaced by the value 8.

    typedef int a;

    Now a is a new type. Just like int, char, double, float are types. The compiler will probably substitute int wherever it sees the type name a, so in that sense it is like a def. However, you can create pointers to type a, declare references to type a, use type a in arrays, etc. all of which you can't do with a plain def. In addition MAX can only be 8 and never anything else. But an instance of a may be declared a constant, in which case it can't be changed from the declared value just like the def, or may be left as a variable, in which case it may be any valid int.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by beely
    you mean typedef and normal def is still the same?
    Eh... Normal def?
    Code:
    typedef int MyOwnInt;
    
    int BeelysVariable;
    MyOwnInt MagosVariable;
    Both variables are integers. You usually don't use typedef on simple things like this, but on larger more complex structures to save time and space.

    Basically, what typedef do is make a new datatype out of something. Having data typed is nice. All of a sudden you can return several variables from a function (see my typedef struct example).
    You can use it like you use a normal datatype, like int and char.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    oh, you mean that typdef is useful for complex code? i get it now.
    then how to define / write the code for the typedef, give me example pls.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The answer's already been given... but here's another example
    Code:
    #include <stdio.h>
    
    typedef int NUMBER;
    
    int main(void)
    {
        NUMBER i;
        
        i = 10;
        
        printf("%d\n", i);
        	
        return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    oh then, thanks you all guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

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