473,473 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

((struct name *)0)->b ?

Hi,

I find that the program gives the offset of the member of the
structure.
So, probably similar to offsetof() .

But, This is the first time i am seeing the typecast with ' 0' . What
does that mean ?

int main()
{
typedef struct name {
long a;
int b;
long c;
}r;
r re = {3,7,5};

printf("%d",((unsigned int)&(( struct name *)0)->b));

printf("%d",((int)&((struct name *)0)->a));
while(1);
}

The output is 4.

But, What does this ((struct name *)0)->b refer to in the above code.
I tried to print this " ((struct name *)0)->b " alone, but that gave
me errors. :(:(
So, i have raised this query here.

Thx in advans,
Karthik Balaguru

Aug 19 '07 #1
6 3049
On 19 Ago, 19:14, karthikbalaguru <karthikbalagur...@gmail.comwrote:
But, What does this ((struct name *)0)->b refer to in the above code.
I tried to print this " ((struct name *)0)->b " alone, but that gave
me errors. :(:(
The expression &(( struct name *)0)->b gives the pointer to the
variabile b inside an hypothetical °struct name° that is allocated at
the address 0, if I'm not wrong :)

The expression ((struct name *)0)->b gives you an error because you
are pointing to a memory zone that you aren't supposed to.

Aug 19 '07 #2
akappa wrote, On 19/08/07 18:43:
On 19 Ago, 19:14, karthikbalaguru <karthikbalagur...@gmail.comwrote:
>But, What does this ((struct name *)0)->b refer to in the above code.
I tried to print this " ((struct name *)0)->b " alone, but that gave
me errors. :(:(

The expression &(( struct name *)0)->b gives the pointer to the
variabile b inside an hypothetical °struct name° that is allocated at
the address 0, if I'm not wrong :)
You are wrong, in theory at least, because it is a null pointer
constant, and there is no requirement for it to be address 0. In
practice it works like that on many implementations, but it is not
required to.
The expression ((struct name *)0)->b gives you an error because you
are pointing to a memory zone that you aren't supposed to.
The original expression also invoked undefined behaviour, i.e. anything
is allowed to happen, even your tie turning in to an overly friendly
snake. If you don't have a tie it could also cause one to appear around
your neck first!

This is why the implementation is required to provide an offsetof macro.
--
Flash Gordon
Aug 19 '07 #3
On 19 Ago, 19:51, Flash Gordon <s...@flash-gordon.me.ukwrote:
You are wrong, in theory at least, because it is a null pointer
constant, and there is no requirement for it to be address 0. In
practice it works like that on many implementations, but it is not
required to.
Mmh, hence if I do:

*((int*)0)

I'm referencing the NULL pointer and not the memory location at zero
address?
It sounds weird :)

Aug 19 '07 #4
In article <11*********************@r29g2000hsg.googlegroups. com>
akappa <an******@gmail.comwrote:
>Mmh, hence if I do:

*((int*)0)

I'm referencing the NULL pointer and not the memory location at zero
address?
It sounds weird :)
Possibly. Perhaps they are the same thing (in fact, "usually" they
*are* the same), but possibly they are different.

Imagine, for a moment, that we create a language that looks exactly
the same as Standard C (C89 or C99, either one) except for one big
change. Instead of defining the "null pointer constant" the way C
does it, we add a new keyword, spelled nil, that produces "the null
pointer constant".

In C-- (or whatever this langauge is called), you do not write:

ptr = malloc(n * sizeof *ptr);
if (ptr == NULL) ...

but rather:

ptr = malloc(n * sizeof *ptr);
if (ptr == nil) ...

and if you want to "nil out" a pointer, you write, e.g.:

...
free(ptr);
ptr = nil;

Now, over here on the left, we happen to have a machine where memory
addresses 0x00000000 through 0xbfffffff are all valid, and any
address from 0xc0000000 through 0xffffffff is invalid. (This
machine can therefore use up to 3 GB of memory.)

If you would like your C-- compiler to be able to access all of
memory, you might want to make "nil" come out to 0xc0000000:

ptr = nil; /* compiles to "mov #0xc0000000,ptr" */

Here, *(int *)0 means "get me the bytes at address zero", while
*(int *)nil means "cause a fault because I attempt to dereference
a nil pointer".

Keeping all that in mind, let us "upgrade" from "C--" to regular C.
Now we have to write expressions such as:

ptr = 0; /* perhaps disgused as "ptr = NULL" */

to set "ptr" to the "nil pointer". But a C compiler can -- and in
the distant past, some did -- compile this to the equivalent of
"mov #0xc0000000,ptr". That is, the integer constant 0, used *as*
a pointer, compiles to the machine's most appropriate "nil pointer"
value, whatever that is.

So, C on this particular machine -- if we design the C compiler
so that it can access all memory -- really does attempt to access
location 0xc0000000 (not location 0) when you write:

*(int *)0

To access memory location zero, you simply use something that is
not an "integer constant zero":

int zero_but_not_integer_constant = 0;
int x;

x = *(int *)zero_but_not_integer_constant;

This gives you access to "memory location zero", on this machine,
with this clever C compiler that uses 0xc0000000 for the internal
representation of NULL.

(Again, such systems really did exist once. The original PR1ME
used a special segment for "null pointers", so that they were not
zero. As it turns out, it was so important to run badly-written
C code that *assumed* null pointers were all-zero-bits that they
modified the hardware, adding a whole new instruction -- TCNP, or
"test C null pointer" -- instead of continuing with this approach.
As a C programmer, you may choose whether to assume that future
systems will be forced to accomodate your bad C code, as PR1ME had
to, or whether to write *good* C code that will work even on "weird"
machines.)

(Note that if you are writing "inherently machine dependent code",
such as code to handle the bootstrap sequence on some particular
machine, it is probably pretty safe to make machine-dependent
assumptions. After all, the code that makes the Zorgblatt 15 boot
is probably not going to work at all on the Frobley 71, no matter
how portable you try to make it. There are cases where *some* of
the boot code can be shared, though -- so you might want to write
the "shareable" part in a portable way, and the machine-dependent
part in "whatever way works".)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 19 '07 #5
akappa wrote, On 19/08/07 19:00:
On 19 Ago, 19:51, Flash Gordon <s...@flash-gordon.me.ukwrote:
>You are wrong, in theory at least, because it is a null pointer
constant, and there is no requirement for it to be address 0. In
practice it works like that on many implementations, but it is not
required to.

Mmh, hence if I do:

*((int*)0)

I'm referencing the NULL pointer and not the memory location at zero
address?
Yes.
It sounds weird :)
That's life.
--
Flash Gordon
Aug 19 '07 #6
On 19 Ago, 20:23, Chris Torek <nos...@torek.netwrote:
[cut]
Thanks for your insightful explanation

Aug 19 '07 #7

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

Similar topics

2
by: Steven T. Hatton | last post by:
typedef struct { unsigned char e_ident; Elf32_Half e_type; Elf32_Half e_machine; Elf32_Word e_version; Elf32_Addr e_entry; Elf32_Off e_phoff; Elf32_Off e_shoff; Elf32_Word e_flags;...
13
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
8
by: J Krugman | last post by:
My compiler complains if I do something like this typedef struct { node *next; } node; but it's OK with typedef struct superfluous_identifier { superfluous_identifier *next;
4
by: acap | last post by:
my problem is converting C to java, pls help..... this the code.... #include <stdio.h> #include <string.h> #include <ctype.h> #define FIRST_NAME_LEN 31 #define SECOND_NAME_LEN 51
4
by: vaiism | last post by:
I am trying to write a string compare function using pointers and dynamic arrays, and am having trouble comparing the individual elements of the array contained with the struct. The code below...
15
by: David Marsh | last post by:
Is there any functional difference (or any other reason to prefer one over the other) between these two methods: typedef struct mystruct { int a; int b; } mystruct; struct mystruct { int a;
21
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first...
6
by: Szabolcs Borsanyi | last post by:
Dear all, I'd like to define a macro that expands to a struct-or-union-specifier. Inside of this struct there shall be a named structure. Alas, the struct tag inside the other unnamed structure...
14
by: eliben | last post by:
Hello, I want to be able to do something like this: Employee = Struct(name, salary) And then: john = Employee('john doe', 34000) print john.salary
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.