Hello everyone!!
I am new to Linux forum and have a question related to typecasting in C related to offset of structures.I have a piece of code and having a problem with this:

void fun(struct c *p)
{
int offset;
struct b *address;
offset=(char *)&((struct c *)(&((struct a*)0)->y)->j)-(char *)((struct a *)0);
address=(struct b *)((char*)&(p->j)-offset);
address->i=400;
address->f=3.14;
address->ch='c';
printf("%u",offset);
}

As you can see that this code is accessible in turbo C but not in Dev C++ and please anybody tell me why is this so and any method to easily typecast structures for finding their offset.

Edited 4 Years Ago by Narue: Added code tags

Please post the structures.

I find this piece of code in a C book and i am unable to get the offset part of this code:

#include<stdio.h>
#include<conio.h>
void fun(struct c *p);
struct a
{
	   struct b
	   {
			  int i;
			  float f;
			  char ch;
				 }x;
				 struct c
				 {
						 int j;
			  float f;
			  char ch;
						  }y;
		}z;
		int main()
		{
		   clrscr();
		 fun(&z.y);
		 printf("\n%d %f %c",z.x.i,z.x.f,z.x.ch);
		 getch();
			return(0);
			 }
void fun(struct c *q)
{
 		   int offset;
 		   struct b *address;
 		   offset=(char *)&((struct c *)(&((struct a *)0)->y)->j)-(char *)((struct a *)0);
 		   address=(struct b *)((char*)&(q->j)-offset);
 		   address->i=1;
 		   address->f=2.1;
		   address->ch='z';
		 
			}

Please help!! Here i want to know that in offset part (char *)is used but when i used (int *)instead of (char *) result differs. Why??? and if you give explanation for

offset=(char *)&((struct c *)(&((struct a *)0)->y)->j)-(char *)((struct a *)0);

It will be a great help.

Edited 4 Years Ago by 3435: Added code tags

As you can see that this code is accessible in turbo C but not in Dev C++

That's because Dev-C++ is properly disallowing a language constraint violation. The address-of operator may only be used on an lvalue. You can use offsetof() to avoid the very hackish calculations that your awful book is encouraging:

#include <stdio.h>
#include <stddef.h>

struct a {
    struct b {
        int i;
        float f;
        char ch;
    } x;
    struct c {
        int j;
        float f;
        char ch;
    } y;
} z;

void fun(struct c *p)
{
    /* Find the offset of the struct c member */
    size_t offset = offsetof(struct a, y);
    
    /* 
        Find the base address of the struct b member 
        since we know it precedes the struct c member
    */
    struct b *address = (struct b*)((char*)p - offset);
    
    /* Hope and pray that what we "know" about struct a is correct */
    address->i = 400;
    address->f = 3.14;
    address->ch = 'c';
}

int main(void)
{
    fun(&z.y);
    printf("\n%d %f %c", z.x.i, z.x.f, z.x.ch);

    return 0;
}

It's still silly and unsafe, but at least the code should build and run on a modern compiler.

Comments
Learned something new today :)

Hello everyone!!
I am new to Linux forum and have a question related to typecasting in C related to offset of structures. <snip>

Typecasting is something that happens to actors...

This article has been dead for over six months. Start a new discussion instead.