473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

A doubt about struct

If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know
whether the align in struct will affect such cast.

Apr 13 '06 #1
7 2239
Alex schrieb:
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know whether the align in struct will affect such cast.


Do not cast from struct s2 to struct s1.

The standard guarantees to you that the address of the first
member of a struct is the same as the address of the struct
itself.

This means you can cast from "struct s2 *" to a pointer to
the type of the first member, "struct s1 *".

There is no conversion defined between the structure types
themselves. If you need the value, you should reinterpret
the address, i.e. for
struct s1 S1;
struct s2 S2 = {.....};
both are equivalent:
S1 = S2.base;
S1 = *((struct s1 *)&S2);
whereas your compiler should complain about
"S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
former an explicit, the latter an implicit conversion to
struct s1).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 13 '06 #2
Alex wrote:
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know
whether the align in struct will affect such cast.


You can only cast scalar types, not composite types like structures, so
I assume that you mean a cast from (struct s2 *) to (struct s1 *). The
C99 standard (6.7.2.1) specifies that a pointer to a structure object,
suitably converted, points to its initial member. Therefore, your
proposed cast is legal and guaranteed to work.

--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality
Apr 13 '06 #3

Michael Mair wrote:
Alex schrieb:
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know whether the align in struct will affect such cast.


Do not cast from struct s2 to struct s1.

The standard guarantees to you that the address of the first
member of a struct is the same as the address of the struct
itself.

This means you can cast from "struct s2 *" to a pointer to
the type of the first member, "struct s1 *".

There is no conversion defined between the structure types
themselves. If you need the value, you should reinterpret
the address, i.e. for
struct s1 S1;
struct s2 S2 = {.....};
both are equivalent:
S1 = S2.base;
S1 = *((struct s1 *)&S2);
whereas your compiler should complain about
"S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
former an explicit, the latter an implicit conversion to
struct s1).


I cannot express my idea well, my intention is pointer cast.
So, if I do like this:
struct s1* p = malloc(sizeof(struct s2));
And then, I use(read or write) some members of struct s1 via the
pointer p, is it OK?

Apr 13 '06 #4
Alex schrieb:
Michael Mair wrote:
Alex schrieb:
If I have two struct. See below:
struct s1
{
int type;
int (*destroy)(struct s1* p);
}

struct s2
{
struct s1 base;
.../* here are some other memebers */
}.

My doubt is that Will the standard C will guarantee the cast from
'struct s2' to 'struct s1' is safe. The problem comes because I don't
know whether the align in struct will affect such cast.


Do not cast from struct s2 to struct s1.

The standard guarantees to you that the address of the first
member of a struct is the same as the address of the struct
itself.

This means you can cast from "struct s2 *" to a pointer to
the type of the first member, "struct s1 *".

There is no conversion defined between the structure types
themselves. If you need the value, you should reinterpret
the address, i.e. for
struct s1 S1;
struct s2 S2 = {.....};
both are equivalent:
S1 = S2.base;
S1 = *((struct s1 *)&S2);
whereas your compiler should complain about
"S1 = (struct s1) S2;" which is equivalent to "S1 = S2" (the
former an explicit, the latter an implicit conversion to
struct s1).


I cannot express my idea well, my intention is pointer cast.
So, if I do like this:
struct s1* p = malloc(sizeof(struct s2));
And then, I use(read or write) some members of struct s1 via the
pointer p, is it OK?


Yes, this is okay. I am not entirely sure what you want to
achieve, though. The above is a little bit too short.
Please give us a little bit more context, e.g. whether you
have a function allocating new storage for struct s2 and you
want to use this just as struct s1 or even a little bit more.
Maybe there is an even better way of doing what you want to
do.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 13 '06 #5

Michael Mair wrote:
Yes, this is okay. I am not entirely sure what you want to
achieve, though. The above is a little bit too short.
Please give us a little bit more context, e.g. whether you
have a function allocating new storage for struct s2 and you
want to use this just as struct s1 or even a little bit more.
Maybe there is an even better way of doing what you want to
do.


What I want to do is something like Interface in Java or abstract class
in C++.
In the struct s1, I will declare some function pointers, which I will
not
assign for them. And in struct s2, I will delcare a object of struct
s1,
then assign for the function pointers of the object. The intention of
doing
all of above is to implement simple OOP using C.( Because In my
enviroment,
I cannot use other language.)
So, if you can provide any information , even a link, about how to
implement simple
OOP, I will appreciate your help very much.

Apr 13 '06 #6
On Thu, 13 Apr 2006 10:52:16 +0200,
Michael Mair <Mi**********@invalid.invalid> wrote
in Msg. <4a************@individual.net>
Yes, this is okay. I am not entirely sure what you want to
achieve, though.


Probably some OO-like stuff in C. The GTK+ toolkit used to be written
like that, and now probably GObject is.

robert
Apr 13 '06 #7
Alex schrieb:
Michael Mair wrote:
Yes, this is okay. I am not entirely sure what you want to
achieve, though. The above is a little bit too short.
Please give us a little bit more context, e.g. whether you
have a function allocating new storage for struct s2 and you
want to use this just as struct s1 or even a little bit more.
Maybe there is an even better way of doing what you want to
do.


What I want to do is something like Interface in Java or
abstract class in C++.
In the struct s1, I will declare some function pointers, which
I will not assign for them. And in struct s2, I will delcare a
object of struct s1, then assign for the function pointers of
the object. The intention of doing all of above is to implement
simple OOP using C.( Because In my enviroment, I cannot use
other language.)
So, if you can provide any information , even a link, about how
to implement simple OOP, I will appreciate your help very much.


There have been several discussions about this in comp.lang.c;
maybe you can find something useful searching the archives:

<http://groups.google.de/groups?as_q=OOP+in+C&num=30&scoring=r&hl=de&as_epq =&as_oq=&as_eq=&as_ugroup=comp.lang.c&as_usubject= &as_uauthors=&lr=&as_qdr=&as_drrb=b&as_mind=1&as_m inm=1&as_miny=2004&as_maxd=17&as_maxm=4&as_maxy=20 06>

The basic technique of "typesafe casting", always remains the
same: You have a pointer to a structure; you know that the
first member of this structure always is or contains (maybe
over several levels) a structure describing a "parent class".
You always can cast a pointer of a "child class" to a pointer
of the "parent class". This of course restricts you to single
inheritance.
Often, all "classes" are derived from one generic "class" which
may even offer a way to find out the "type" of the "class" it
is a member of. In this case, it is safe to cast "from small to
large", i.e. from the pointer of the "parent class" to a pointer
to the "child class"; this can become the source of merry
confusion if not done right.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
4
by: dam_fool_2003 | last post by:
I am just a beginner in tree data – struct. I have this little doubt. Left node ‘weights' lesser than the right one. I have seen, so far it is algorithm implementations. But why not vice-versa that...
20
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
9
by: grid | last post by:
Hi, I have a function which takes a variable number of arguments.It then calls va_start macro to initialize the argument list.But here in my case I have a special builtin va_start provided by the...
2
by: priya | last post by:
Hi All, Currently I am working in Socket Progrm written in C. Here i pasted Both Server and client code.. Server.c code
22
by: srivatsan_b | last post by:
Hi, Can somebody explain whether an explicit typecast is mandatory while calling memset function for a structure? like in the following code snapshot..... struct some_structure x;...
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
5
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.