type casting

R

Roman Mashak

Hello, All!

I met this code recently on some open source sites. What may be the point of
using such construction:

typedef struct cmd
{
unsigned int cmdack;
unsigned int code;
unsigned int data[1];
} CMD;

....

int proc_cmd()
{
char msg[]={"CMD"};
int lenth = strlen(msg);
CMD *t, *r;
int st[100];
int sr[100];

t = (CMD *)st;
r = (CMD *)sr;
...
/* process msg */
if(strncmp((char *)&r->cmdack, msg, len) == 0
{
....
}
}

Why to define structure's member as 'unsigned int' and then implicitly do
type casting?

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
T

Tomás

Roman Mashak posted:
Hello, All!

I met this code recently on some open source sites. What may be the
point of using such construction:

typedef struct cmd
{
unsigned int cmdack;
unsigned int code;
unsigned int data[1];
} CMD;



You can define more memory pass the end of the structure and then use the
last array member to access them:

#include <...

typedef struct Monkey {
unsigned a;
unsigned b;
unsigned vars[1];
};

int main(void)
{
Monkey * const pmonkey = malloc( sizeof(Monkey) + 10 );

pmonkey->vars[7] = 53;
}

Why to define structure's member as 'unsigned int' and then implicitly
do type casting?

No reason, it's stupid code. It also depends on the fact that there's no
padding between the structure's members.


-Tomás
 
T

Tomás

malloc( sizeof(Monkey) + 10 );


Opps.

malloc( sizeof(Monkey) + sizeof( int[10] ) );


-Tomás
 
P

pete

Roman said:
Hello, All!

I met this code recently on some open source sites. What may be the point of
using such construction:

typedef struct cmd
{
unsigned int cmdack;
unsigned int code;
unsigned int data[1];
} CMD;

...

int proc_cmd()
{
char msg[]={"CMD"};
int lenth = strlen(msg);
CMD *t, *r;
int st[100];
int sr[100];

t = (CMD *)st;
r = (CMD *)sr;
...
/* process msg */
if(strncmp((char *)&r->cmdack, msg, len) == 0
{
....
}
}

Why to define structure's member as
'unsigned int' and then
implicitly do type casting?

"cast" is the right way to say "implicitly do type casting".

The shown code,
suggests that r should be defined with type pointer to char.
As the code is,
((char *)&r->cmdack) is equal to (char *)r.
 
R

Richard Heathfield

pete said:
"cast" is the right way to say "implicitly do type casting".

No, it isn't. An implicit conversion is not a cast. A cast is an explicit
conversion, so if it's implicit, it's not casting.
 
P

pete

Richard said:
pete said:


No, it isn't.

Yes, it is.
An implicit conversion is not a cast.
A cast is an explicit conversion,
so if it's implicit, it's not casting.

The right way to say that,
is the way which applies to the code that you snipped:

"A cast is an explicit conversion,
so if it's a cast, it's not implicit."
 
R

Richard Heathfield

pete said:
The right way to say that,
is the way which applies to the code that you snipped:

"A cast is an explicit conversion,
so if it's a cast, it's not implicit."

Yes. I apologise for my incorrect "correction".
 
R

Roman Mashak

Hello, pete!
You wrote on Mon, 22 May 2006 12:11:54 GMT:

p> The shown code,
p> suggests that r should be defined with type pointer to char.
p> As the code is,
p> ((char *)&r->cmdack) is equal to (char *)r.
Yes, but what not to use 'char' members? Is the only reason - to eliminate
padding within structure?

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
R

Roman Mashak

Hello, Tomás!
You wrote on Mon, 22 May 2006 11:54:10 GMT:

T> You can define more memory pass the end of the structure and then use
T> the last array member to access them:
Is it legal and valid in standard C (if speaking about C89) ?

T> #include <...

T> typedef struct Monkey {
T> unsigned a;
T> unsigned b;
T> unsigned vars[1];
T> };

T> int main(void)
T> {
T> Monkey * const pmonkey = malloc( sizeof(Monkey) + 10 );

T> pmonkey->vars[7] = 53;
T> }

T> <snip code>

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
P

pete

Roman said:
Hello, pete!
You wrote on Mon, 22 May 2006 12:11:54 GMT:

p> The shown code,
p> suggests that r should be defined with type pointer to char.
p> As the code is,
p> ((char *)&r->cmdack) is equal to (char *)r.
Yes, but what not to use 'char' members?
Is the only reason - to eliminate
padding within structure?

I don't really understand why the code is written
exactly the way that it is.
 

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

Similar Threads

array of size 1 10
tolower() and toupper() 8
type casting 9
some misunderstanding of pointers 5
type casting 5
problem with macro 6
Type-punning / casting problem 20
remove entry from text file [NOT A HOMEWORK] 36

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top