Can I use that sentence ? Thanks

Discussion in 'C Programming' started by yezi, Nov 4, 2005.

  1. yezi

    yezi Guest

    hi all :

    I have question regarding to the structure which include an other
    structure .

    typedef struct routingTableEntry
    {
    int destNode;
    int hopCount;
    int nextNode;
    int path[5];
    }TABLEENTRY;

    typedef struct routetable
    {
    struct routingTableEntry * entry;
    struct routetable * next;
    } ROUTETABLE;


    Can I write the sentence like the following :

    tmp = (ROUTETABLE*)malloc(sizeof(ROUTETABLE));

    if (tmp == NULL) {
    printf("Can't initialize routing table.\n");
    exit(1);
    }

    tmp -> entry -> destNode = selfNumber;
    printf("before the hopcount\n"); //Problem here
    tmp -> entry -> hopCount = 0;
    tmp -> entry -> nextNode = 0;

    If can not , How can I assign the numbers to the sub structure?

    Thanks for any comments.

    bin YE
     
    yezi, Nov 4, 2005
    #1
    1. Advertising

  2. yezi

    Mike Wahler Guest

    "yezi" <> wrote in message
    news:...
    > hi all :
    >
    > I have question regarding to the structure which include an other
    > structure .


    None of your structures include other structures.
    However 'struct routetable' does include pointers
    to two struct types. More below.

    > typedef struct routingTableEntry
    > {
    > int destNode;
    > int hopCount;
    > int nextNode;
    > int path[5];
    > }TABLEENTRY;
    >
    > typedef struct routetable
    > {
    > struct routingTableEntry * entry;
    > struct routetable * next;
    > } ROUTETABLE;
    >
    >
    > Can I write the sentence like the following :
    >



    > tmp = (ROUTETABLE*)malloc(sizeof(ROUTETABLE));


    ROUTETABLE *tmp = malloc(sizeof *tmp);

    /* Be sure you've #included <stdlib.h> for declaration
    of 'malloc()' and 'exit()' */

    >
    > if (tmp == NULL) {
    > printf("Can't initialize routing table.\n");


    /* Be sure you've #included <stdio.h> for declaration
    of 'printf()' */

    > exit(1);
    > }
    >
    > tmp -> entry -> destNode = selfNumber;


    'tmp->entry' does not point anywhere. You need to
    define or allocate a 'struct routingTableEntry' object
    and assign its address to 'tmp->entry'

    tmp->entry = malloc(sizeof *entry));

    > printf("before the hopcount\n"); //Problem here
    > tmp -> entry -> hopCount = 0;
    > tmp -> entry -> nextNode = 0;
    >
    > If can not , How can I assign the numbers to the sub structure?


    By first creating such a structure. Your code doesn't define
    or allocate one.

    Finally, don't forget to 'free()' your 'malloc()'ed memory
    when you're done with it.

    -Mike
     
    Mike Wahler, Nov 4, 2005
    #2
    1. Advertising

  3. yezi

    Malcolm Guest

    "Mike Wahler" <> wrote
    > tmp->entry = malloc(sizeof *entry));
    >

    tmp->entry = malloc( sizeof *tmp->entry );

    IMO

    tmp->entry = malloc( sizeof(TABLE_ENTRY));

    is easier on the eye
     
    Malcolm, Nov 5, 2005
    #3
  4. yezi

    Mike Wahler Guest

    "Malcolm" <> wrote in message
    news:dkhmkp$5mv$-infra.bt.com...
    >
    > "Mike Wahler" <> wrote
    >> tmp->entry = malloc(sizeof *entry));
    >>

    > tmp->entry = malloc( sizeof *tmp->entry );
    >
    > IMO
    >
    > tmp->entry = malloc( sizeof(TABLE_ENTRY));
    >
    > is easier on the eye


    But what I wrote is imo more flexible and maintainable
    (The type can be changed with no modification to the
    call to 'malloc()')

    I also find what I wrote 'easier on the eye' because it's a
    well known and much used idiom with which I'm familiar.

    But 'to each his own'. (If I found your form in code
    for which I'm responsible, I wouldn't fire you, but I'd
    probably change it. :) )

    -Mike
     
    Mike Wahler, Nov 5, 2005
    #4
  5. yezi

    pete Guest

    Mike Wahler wrote:
    >
    > "Malcolm" <> wrote in message
    > news:dkhmkp$5mv$-infra.bt.com...
    > >
    > > "Mike Wahler" <> wrote
    > >> tmp->entry = malloc(sizeof *entry));


    > But what I wrote is imo more flexible and maintainable
    > (The type can be changed with no modification to the
    > call to 'malloc()')
    >
    > I also find what I wrote 'easier on the eye' because it's a
    > well known and much used idiom with which I'm familiar.


    Quoted in this post? I don't recognize it.
    After thinking a little,
    I see that your C statment should be correct.

    > But 'to each his own'. (If I found your form in code
    > for which I'm responsible, I wouldn't fire you, but I'd
    > probably change it. :) )


    The first C statement that Malcom wrote,
    is what I recognize at a glance, as being the clc idiom.

    > > tmp->entry = malloc( sizeof *tmp->entry );


    --
    pete
     
    pete, Nov 5, 2005
    #5
  6. yezi

    Mike Wahler Guest

    "pete" <> wrote in message
    news:...
    > Mike Wahler wrote:
    >>
    >> "Malcolm" <> wrote in message
    >> news:dkhmkp$5mv$-infra.bt.com...
    >> >
    >> > "Mike Wahler" <> wrote
    >> >> tmp->entry = malloc(sizeof *entry));

    >
    >> But what I wrote is imo more flexible and maintainable
    >> (The type can be changed with no modification to the
    >> call to 'malloc()')
    >>
    >> I also find what I wrote 'easier on the eye' because it's a
    >> well known and much used idiom with which I'm familiar.

    >
    > Quoted in this post?


    Yes:
    More generalized:

    T *id =malloc(sizeof *id));

    > I don't recognize it.


    Shrug.

    > After thinking a little,
    > I see that your C statment should be correct.


    OF course it is.

    >
    >> But 'to each his own'. (If I found your form in code
    >> for which I'm responsible, I wouldn't fire you, but I'd
    >> probably change it. :) )

    >
    > The first C statement that Malcom wrote,
    > is what I recognize at a glance, as being the clc idiom.


    We recognize different things. Whatever.

    -Mike
     
    Mike Wahler, Nov 5, 2005
    #6
  7. yezi

    pete Guest

    Mike Wahler wrote:
    >
    > "pete" <> wrote in message
    > news:...
    > > Mike Wahler wrote:
    > >>
    > >> "Malcolm" <> wrote in message
    > >> news:dkhmkp$5mv$-infra.bt.com...
    > >> >
    > >> > "Mike Wahler" <> wrote
    > >> >> tmp->entry = malloc(sizeof *entry));


    > > The first C statement that Malcom wrote,
    > > is what I recognize at a glance, as being the clc idiom.

    >
    > We recognize different things. Whatever.


    There's thinking invloved in writing the C statement
    that you have there.
    You've handled it as a special case, though it isn't.
    You parsed the left operand of the assignment operator
    when you didn't have to.

    The simplest way,
    is to copy the left operand of the assignment operator,
    stick a * operator in front of it, and let the result of that
    be the operand of the sizeof operator.

    --
    pete
     
    pete, Nov 6, 2005
    #7
  8. yezi

    Netocrat Guest

    On Sat, 05 Nov 2005 13:38:45 +0000, pete wrote:
    [replying to Mike Wahler]
    > After thinking a little,
    > I see that your C statment should be correct.


    I think you're referring to this statement:
    tmp->entry = malloc(sizeof *entry));

    I've both thought about it and tried to compile it and I can't find it
    correct at all - even with the extra closing brace removed. I wouldn't
    have gone to that trouble had I not become accustomed to pete's posts
    being well-considered or self-corrected in short order, so I wonder if I'm
    missing something obscure. There seems to be neither a type nor a
    variable named "entry" in scope.

    Here are the error messages gcc returns:

    yezi.c: In function `main':
    yezi.c:30: error: `entry' undeclared (first use in this function)
    yezi.c:30: error: (Each undeclared identifier is reported only once
    yezi.c:30: error: for each function it appears in.)

    and here's the complete code with only minor differences from the OP's
    code to make it compilable (the commented alternative line compiles fine):

    #include <stdlib.h>
    #include <stdio.h>

    typedef struct routingTableEntry
    {
    int destNode;
    int hopCount;
    int nextNode;
    int path[5];
    }TABLEENTRY;

    typedef struct routetable
    {
    struct routingTableEntry * entry;
    struct routetable * next;
    } ROUTETABLE;

    int main(void)
    {
    int selfNumber = 0;

    ROUTETABLE *tmp = malloc(sizeof *tmp);

    if (tmp == NULL) {
    printf("Can't initialize routing table.\n");
    exit(EXIT_FAILURE);
    }

    /* tmp->entry = malloc(sizeof *tmp->entry);
    */ tmp->entry = malloc(sizeof *entry);
    if (tmp -> entry == NULL) {
    printf("malloc failed for tmp->entry.\n");
    exit(EXIT_FAILURE);
    }

    tmp -> entry -> destNode = selfNumber;
    printf("before the hopcount\n");
    tmp -> entry -> hopCount = 0;
    tmp -> entry -> nextNode = 0;

    return 0;
    }

    --
    http://members.dodo.com.au/~netocrat
     
    Netocrat, Nov 6, 2005
    #8
  9. yezi

    Mike Wahler Guest

    "Netocrat" <> wrote in message
    news:p...
    > On Sat, 05 Nov 2005 13:38:45 +0000, pete wrote:
    > [replying to Mike Wahler]
    >> After thinking a little,
    >> I see that your C statment should be correct.

    >
    > I think you're referring to this statement:
    > tmp->entry = malloc(sizeof *entry));
    >
    > I've both thought about it and tried to compile it and I can't find it
    > correct at all - even with the extra closing brace removed. I wouldn't
    > have gone to that trouble had I not become accustomed to pete's posts
    > being well-considered or self-corrected in short order, so I wonder if I'm
    > missing something obscure. There seems to be neither a type nor a
    > variable named "entry" in scope.


    It was of course a simple typo, and should have been:

    tmp->entry = malloc(sizeof *tmp->entry));

    Sorry for any confusion.

    -Mike
     
    Mike Wahler, Nov 6, 2005
    #9
  10. yezi

    pete Guest

    Netocrat wrote:
    >
    > On Sat, 05 Nov 2005 13:38:45 +0000, pete wrote:
    > [replying to Mike Wahler]
    > > After thinking a little,
    > > I see that your C statment should be correct.

    >
    > I think you're referring to this statement:
    > tmp->entry = malloc(sizeof *entry));
    >
    > I've both thought about it and tried to compile it and I can't find it
    > correct at all - even with the extra closing brace removed.


    Wake up, Neto...
    The Matrix has you...

    > I wouldn't
    > have gone to that trouble had I not become accustomed to pete's posts
    > being well-considered or self-corrected in short order,
    > so I wonder if I'm missing something obscure.


    That's because I very usually get caught when I'm wrong.
    That gets old fast.
    Good catch!

    --
    pete
     
    pete, Nov 6, 2005
    #10
  11. yezi

    Simon Biber Guest

    Mike Wahler wrote:
    > "Netocrat" <> wrote in message
    > news:p...
    >
    >>On Sat, 05 Nov 2005 13:38:45 +0000, pete wrote:
    >>[replying to Mike Wahler]
    >>
    >>>After thinking a little,
    >>>I see that your C statment should be correct.

    >>
    >>I think you're referring to this statement:
    >>tmp->entry = malloc(sizeof *entry));
    >>
    >>I've both thought about it and tried to compile it and I can't find it
    >>correct at all - even with the extra closing brace removed. I wouldn't
    >>have gone to that trouble had I not become accustomed to pete's posts
    >>being well-considered or self-corrected in short order, so I wonder if I'm
    >>missing something obscure. There seems to be neither a type nor a
    >>variable named "entry" in scope.

    >
    >
    > It was of course a simple typo, and should have been:
    >
    > tmp->entry = malloc(sizeof *tmp->entry));


    You still have an extra ')'.

    > Sorry for any confusion.
    >
    > -Mike


    --
    Simon.
     
    Simon Biber, Nov 17, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. huhu

    Who can explain the sentence for me?

    huhu, Feb 29, 2004, in forum: C Programming
    Replies:
    8
    Views:
    323
  2. www
    Replies:
    2
    Views:
    729
    Andreas Leitgeb
    Jan 30, 2007
  3. yezi

    What is the sentence mean?Thanks

    yezi, Nov 28, 2005, in forum: C Programming
    Replies:
    3
    Views:
    389
  4. sword
    Replies:
    2
    Views:
    337
    Sumit RAJAN
    Aug 3, 2006
  5. Raymundo
    Replies:
    1
    Views:
    159
    Raymundo
    Apr 24, 2011
Loading...

Share This Page