Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Can I use that sentence ? Thanks

Reply
Thread Tools

Can I use that sentence ? Thanks

 
 
yezi
Guest
Posts: n/a
 
      11-04-2005
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

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      11-04-2005

"yezi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      11-05-2005

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

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

IMO

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

is easier on the eye


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-05-2005

"Malcolm" <(E-Mail Removed)> wrote in message
news:dkhmkp$5mv$(E-Mail Removed)-infra.bt.com...
>
> "Mike Wahler" <(E-Mail Removed)> 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


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-05-2005
Mike Wahler wrote:
>
> "Malcolm" <(E-Mail Removed)> wrote in message
> news:dkhmkp$5mv$(E-Mail Removed)-infra.bt.com...
> >
> > "Mike Wahler" <(E-Mail Removed)> 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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-05-2005

"pete" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Wahler wrote:
>>
>> "Malcolm" <(E-Mail Removed)> wrote in message
>> news:dkhmkp$5mv$(E-Mail Removed)-infra.bt.com...
>> >
>> > "Mike Wahler" <(E-Mail Removed)> 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


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-06-2005
Mike Wahler wrote:
>
> "pete" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Mike Wahler wrote:
> >>
> >> "Malcolm" <(E-Mail Removed)> wrote in message
> >> news:dkhmkp$5mv$(E-Mail Removed)-infra.bt.com...
> >> >
> >> > "Mike Wahler" <(E-Mail Removed)> 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
 
Reply With Quote
 
Netocrat
Guest
Posts: n/a
 
      11-06-2005
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
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      11-06-2005

"Netocrat" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      11-06-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
How to use regex to split a sentence with different spaces? www Java 2 01-30-2007 09:49 PM
Cakewalk Pro Audio 9 (running problem) [THANKS] [ MORE THANKS] con't beenthere Computer Support 2 09-07-2006 08:58 AM
What is the sentence mean?Thanks yezi C Programming 3 11-28-2005 05:47 PM
Thanks, thanks a lot Rick Computer Support 0 05-05-2004 04:04 AM



Advertisments