structure compare

This is a discussion on structure compare within the C Programming forums, part of the General Programming Boards category; what different between : ==== struct *structptr; -vs- struct structptr; ?? and then how about the used for the typedef ...

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

    structure compare

    what different between :
    ====

    struct *structptr;

    -vs-

    struct structptr;

    ??

    and then how about the used for the typedef int <variable>, and int <variable> ??

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Could you describe your question in a different manner? I find it difficult to understand.

    p.s. I love your avatar, it's adorable.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One is a pointer to an object. One isn't.
    [edit] Or they would be, if that was valid code... [/edit]

    Quzah.
    Last edited by quzah; 10-24-2002 at 09:47 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    [prelude] p.s. I love your avatar, it's adorable
    reply : oh heee, my friends copy it to me. LOL thx

    --

    [quzah]
    reply :
    meaning ? actually the code will found mostly on the stucture.

    name *structptr;

    >> is it used for pointer? pointer to pointer ? and how're they work ?

    name structptr;

    >> is it also pointer too? or just a reference? ermm... so isn't the upper code 's function still the same, but the usage is different? could you describe more detail with the source code? pls...

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    here's the eg. u wanted

    Code:
    #include<stdio.h>
     #include<alloc.h>
    typedef struct data
     {
     int  num;
     }object;
    
    void main()
      {
    	 object *first;
    	 object second;
    	 puts("Enter two numbers :");
                     first= (object *)malloc(4);
    	 scanf("%d %d",&first->num,&second.num);
    	 puts("Entered data :");
    	 printf("%d %d",first->num,second.num);
     }
    " object " is the typedefinition.
    Hadn't it been used u would always have to go for long stmts like

    struct data *first,second ;
    It's also useful when u go for type qualifiers like unsigned long int...

    Code:
    object *first; 
    object second;
    the first stmt is a pointer to the structure(in this case ,object)
    i.e first will contain the address of a structure.u need to use the arrow operator to access the members in this case.

    the second one is an ordinary structure variable.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>here's the eg. u wanted
    No it's not.

    >>void main
    void main is wrong, please don't promote it's use.

    >>#include<alloc.h>
    What are you using it for? why not stdlib.h?

    >>first= (object *)malloc(4);
    You malloc'd 4 bytes. How do you know that sizeof(object) is only 4 bytes? Your use of malloc is wrong here, it should be
    >>first=malloc(sizeof(*first));

    >>scanf("%d %d",&first->num,&second.num);
    Don't forget to check the return from scanf() to ensure it did what you asked it to.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Hammer
    >>first= (object *)malloc(4);
    You malloc'd 4 bytes. How do you know that sizeof(object) is only 4 bytes? Your use of malloc is wrong here, it should be
    >>first=malloc(sizeof(*first));
    I'd say it's better to use sizeof(object). To use the actual structure, not the instance of it.
    But it's basically the same thing...
    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
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Magos
    I'd say it's better to use sizeof(object). To use the actual structure, not the instance of it.
    But it's basically the same thing...
    Fair enough.... there's another thread on that particular matter somewhere... either way is fine by me. I was just highlighting the fact that hard coding a numeric as the size of the structure is wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'd say it's better to use sizeof(object).
    This is not just a style issue, many people prefer

    T *mem = malloc ( sizeof *mem );

    because it minimizes the number of items you need to change with an update. This is not as much an issue with a text editor that can find and replace text, but it is still considered good style. Now, it still is a pretty big style issue even though there are distinct maintanability reasons for not using sizeof ( structure ), so use what you feel comfortable with. Though I can pretty much guarantee that I or someone else on comp.lang.c will suggest you use sizeof ( *instance ) for this and other reasons.

    I now return you to your regularly scheduled thread.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    " object " is the typedefinition.
    object is a type alias to the type struct data.
    A type is a set of values and a set of operations. So like for a type int, the set of values is
    [INT_MIN, INT_MAX] and the set of operations is
    {+, -, +=, -=, ++, --, *, /, %}

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

    wrong!

    this statement is wrong ..
    >> scanf ("%d",&board->value); //something like that!

    correction is ...

    >> scanf ("%d",&(board->value));

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: wrong!

    Originally posted by beely
    this statement is wrong ..
    >> scanf ("%d",&board->value); //something like that!

    correction is ...

    >> scanf ("%d",&(board->value));
    No it isn't. The first is not incorrect. It's perfectly valid.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    this statement is wrong ..
    >> scanf ("%d",&board->value); //something like that!

    correction is ...

    >> scanf ("%d",&(board->value));
    Both calls to scanf are equivalent. The -> operator has higher precedence than the address-of operator, so the indirection is always performed first unless you use parens to force taking the address first:

    (&board)->value

    Which in this case would be an error, but you could be creative and do something like this to make it work:

    scanf ( "%d", &(*&t)->value );

    Though I don't recommend it.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 06:46 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. How to compare structure elements
    By khpuce in forum C Programming
    Replies: 6
    Last Post: 04-10-2005, 12:40 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM

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