Can I use that sentence ? Thanks

Y

yezi

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
 
M

Mike Wahler

yezi said:
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);

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

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
 
M

Malcolm

Mike Wahler said:
tmp->entry = malloc(sizeof *entry));
tmp->entry = malloc( sizeof *tmp->entry );

IMO

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

is easier on the eye
 
M

Mike Wahler

Malcolm said:
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
 
P

pete

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.
 
M

Mike Wahler

pete said:
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.
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
 
P

pete

Mike said:
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.
 
N

Netocrat

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;
}
 
M

Mike Wahler

Netocrat said:
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
 
P

pete

Netocrat said:
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!
 
S

Simon Biber

Mike said:
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 ')'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top