473,394 Members | 1,759 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,394 software developers and data experts.

syntax: struct in a struct

I have got these two structs where the second is embedding the first:

typedef struct{
int i1;
int i2;
}struct1;

typedef struct{
int i3;
struct1 s;
}struct2;

Then, the second struct is passed as a void pointer argument in the
callback function f:

void f(void* struct_of_type_struct2_expected){...};

My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2

but have to cast s into struct1 explicitly first:

(struct1)(((struct2*)struct_of_type_struct2_expect ed)->s).i2
Felix

Mar 23 '06 #1
9 2026
On 2006-03-23, fk****@googlemail.com <fk****@googlemail.com> wrote:
I have got these two structs where the second is embedding the first:

typedef struct{
int i1;
int i2;
}struct1;

typedef struct{
int i3;
struct1 s;
}struct2;

Then, the second struct is passed as a void pointer argument in the
callback function f:

void f(void* struct_of_type_struct2_expected){...};

My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2
Why can't you? What error message are you getting?
but have to cast s into struct1 explicitly first:

(struct1)(((struct2*)struct_of_type_struct2_expect ed)->s).i2
Felix

Mar 23 '06 #2
"fk****@googlemail.com" <fk****@googlemail.com> wrote:
I have got these two structs where the second is embedding the first:

typedef struct{
int i1;
int i2;
}struct1;

typedef struct{
int i3;
struct1 s;
}struct2;

Then, the second struct is passed as a void pointer argument in the
callback function f:

void f(void* struct_of_type_struct2_expected){...};

My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2


You can. Are you sure you're using a C compiler?

Richard
Mar 23 '06 #3
Jordan Abel wrote:
On 2006-03-23, fk****@googlemail.com <fk****@googlemail.com> wrote:


[...]
My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2


Why can't you? What error message are you getting?


Unfortunatelly I don't get an error message. It just turned out that i2
never contained the expected results, and explicitly casting s before
surrounded this problem.

Felix

Mar 23 '06 #4

Richard Bos wrote:
"fk****@googlemail.com" <fk****@googlemail.com> wrote:


[...]
My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2


You can. Are you sure you're using a C compiler?


Hm. I am using gcc-3.4.2 (mingw-special).

Felix

Mar 23 '06 #5
"fk****@googlemail.com" <fk****@googlemail.com> wrote:
Richard Bos wrote:
"fk****@googlemail.com" <fk****@googlemail.com> wrote:

My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2


You can. Are you sure you're using a C compiler?


Hm. I am using gcc-3.4.2 (mingw-special).


And you're compiling as C, not as, say, C++? There are areas surrounding
conversion where C++ makes different demands from C; I do not know
whether this is one of them.
If you're compiling C, post a complete program, as small as you can cut
it, which exhibits the problem. It doesn't appear in my test:

#include <stdio.h>

typedef struct {
int i1;
int i2;
} struct1;

typedef struct {
int i3;
struct1 s;
} struct2;

void f(void *str2)
{
printf("%d\n", ((struct2 *)str2)->s.i2);
return;
}

int main(void) {
struct2 s2={3, {1,2}};

f(&s2);

getchar();
return 0;
}

I get an output of 2, as expected. This with Dev-C++, which is also gcc
plus MinGW.

Richard
Mar 23 '06 #6

fk****@googlemail.com wrote:
Richard Bos wrote:
"fk****@googlemail.com" <fk****@googlemail.com> wrote:


[...]
My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2


You can. Are you sure you're using a C compiler?


Hm. I am using gcc-3.4.2 (mingw-special).


I just tried it on exact same compiler, and it works.

Here's the exact code I tried:

#include<stdio.h>

typedef struct {
int i1;
int i2;
} struct1;

typedef struct{
int i3;
struct1 s;
} struct2;

void f(void* struct_of_type_struct2_expected)
{
((struct2*)struct_of_type_struct2_expected)->s.i2 = 42;
}

int main(void)
{
struct2 s2;

f(&s2);

printf("%d\n", s2.s.i2);

return 0;
}

Mar 23 '06 #7
Richard Bos wrote:
And you're compiling as C, not as, say, C++?
Yes, I compile as C.

If you're compiling C, post a complete program, as small as you can cut
Hm, I'll see what I can extract but this seems part of the problem
itself since the code I posted seems to be the relevant one as far as I
unterstand things here. So, it will take a little to extract the right
code from my program (which is quite large) to still generate the
error.
it, which exhibits the problem. It doesn't appear in my test:


Thanks so far for testing it.

Felix

Mar 23 '06 #8

<fk****@googlemail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
I have got these two structs where the second is embedding the first:

typedef struct{
int i1;
int i2;
}struct1;

typedef struct{
int i3;
struct1 s;
}struct2;

Then, the second struct is passed as a void pointer argument in the
callback function f:

void f(void* struct_of_type_struct2_expected){...};

My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2

This is correct and works with two compilers for me.
but have to cast s into struct1 explicitly first:

(struct1)(((struct2*)struct_of_type_struct2_expect ed)->s).i2


Is that actually what you used? This is illegal in ANSI/ISO C. You can't
cast to a struct, union, array or function.

The precedence of the direct '.' and indirect '->' component selection
operators should be the same and both left associative. If the precedence
rules are incorrect for your compiler, you'd need this (without the cast to
struct1):

(((struct2*)struct_of_type_struct2_expected)->s).i2

If you want to use the cast to struct1, it would be written like so:

((struct1*)&(((struct2*)struct_of_type_struct2_exp ected)->s))->i2

You must cast to a pointer to struct1 since casts to a struct are illegal.
Also, notice that you must take the address of struct s before casting to a
pointer to struct1 and now use the indirection operator for i2.
Rod Pemberton
Mar 23 '06 #9
fk****@googlemail.com wrote:
Richard Bos wrote:
And you're compiling as C, not as, say, C++?


Yes, I compile as C.

If you're compiling C, post a complete program, as small as you can cut


Hm, I'll see what I can extract but this seems part of the problem
itself since the code I posted seems to be the relevant one as far as I
unterstand things here. So, it will take a little to extract the right
code from my program (which is quite large) to still generate the
error.
it, which exhibits the problem. It doesn't appear in my test:


Since your code compiles and since your construct is legal, it's
probable that some other area of your program is trashing i2.

Mar 23 '06 #10

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

Similar topics

1
by: Davide Pippa | last post by:
Hi! I'm trying to compile the code above code (actually a similar one...), and I get this error (with gcc 3.2.2) : trick_1.cpp:16: template parameters not used in partial specialization:...
4
by: nonzero | last post by:
I was hoping someone could explain the Syntax of the second argument of the bind() function. bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); .....................(struct...
5
by: Sönke Tesch | last post by:
Hi everybody, I have a problem with the following piece of code: 141: /* A data block to manage a single log target: */ 142: typedef struct { 143: apr_reslist_t *dbs; /* connection pool */...
27
by: Adam Warner | last post by:
Hi all, In the code snippet below I successfully determine the address of val1:* struct o val1=l_SYM_2B(&a).o; print_aesthetic(&val1); The structure o is heavyweight. I understand...
16
by: danu | last post by:
I have a structure : typedef struct{ char magicNum; int width; int height; int maxGrey; int pixels; } ImageT;
9
by: johan.tibell | last post by:
I'm in the process of writing an interpreter for lambda calculus (i.e. a small functional programming language) in C. I've previously written one in Haskell so I understand at least some of the...
3
by: gmarkowsky | last post by:
Hi all, I'm teaching myself C, and I've come upon syntax in a program (Ping) that I don't understand. For instance, we have struct sockaddr_in *to = (struct sockaddr_in *) &whereto; and...
12
by: Yusuf | last post by:
I'm sorry for the second post in as many hours, and both about structs. The code below compiles. The variables test1 and test2 are structures of datatype teststruct1 and teststruct2. The size of...
2
by: berrylthird | last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example: var myInstance:myClass = new...
2
by: Gunners | last post by:
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> struct st_base { char name; int roll_no; int s1,s2,s3,s4,mks; float percentage;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.