473,326 Members | 2,104 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,326 software developers and data experts.

Pointers to fields in a struct

JS
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));

Now I have this:
state_pointer = (int *) &pcb_pointer->state;
state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??

JS

Nov 14 '05 #1
6 2314
JS wrote:
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));

Now I have this:
state_pointer = (int *) &pcb_pointer->state;
state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??

JS


The cast "(int *)" in the assignment tells the compiler to
do its best job to convert a "pointer to jmp_buf" to a
pointer to integer. The details of the conversion are
compiler specific. The results are undefined behavior,
and most-likely platform dependent.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Nov 14 '05 #2


JS wrote:
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));
This would be better as

pcb_pointer = malloc(sizeof *pcb_pointer);
Now I have this:
state_pointer = (int *) &pcb_pointer->state;
state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??


Unsafely. You're likely to get away with it, because
in a typical implementation a `jmp_buf' will contain things
whose alignment is at least as strict as that of an `int'.
But it seems a gratuitous piece of uncleanliness; why not
use `jmp_buf *state_pointer;' instead?

--
Er*********@sun.com

Nov 14 '05 #3
JS wrote:
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));

Now I have this:
state_pointer = (int *) &pcb_pointer->state;
state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??
...


Well, the first question is why the hell are you (or the author of the
code) trying to use an 'int*' pointer to point to a 'jmp_buf' object?

At the same time, C standard guarantees that if 'jmp_buf' is an
aggregate type and the first member of that aggregate type is of type
'int', then the resultant pointer will point to that first member. That
would be portable. Otherwise, you are probably dealing with a hack that
can only be explained by some platform-specific reasons.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #4
In article <d1**********@news.net.uni-c.dk>, JS <dsa.@asdf.com> wrote:
I have [a pointer to struct containing a jmp_buf named "state", and also]int *state_pointer; ...state_pointer = (int *) &pcb_pointer->state;

state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??


It cannot. Unless perhaps it can; see below. But the cast -- the
thing in parentheses -- tells the compiler: "shut up about this
code that is probably wrong, because I, the programmer, know much
better than you and I want to do it anyway."

As a general rule, pointer casts in C mean that whoever wrote the
code was either very smart, or rather careless. :-) Most C code
should be cast-free. There are some exceptions, and casts from
int to double (or vice versa), for instance, are pretty mild and
minor. It is pointer casts that get people into trouble. If you
can write the code without a cast, and compile it without complaint
from the compiler, you will usually be better off than if you use
a cast and shut the compiler up even if it is right and your code
is wrong after all.

Now, if we take out the cast, *and* take out the unary "&" operator:

state_pointer = pcb_pointer->state;

and if the code still compiles quietly, then we have learned
something about the implementation in question. We know -- from
the C standard, which says so -- that "jmp_buf" is a typedef for
an array type. We do not know what the array type is, and it may
(and probably does) change from one compiler to the next. But it
could well be a typedef for "array N of int", for some constant N,
and in fact this turns out to be fairly common. If you have a text
<setjmp.h> that you can read, you might peek at it and find:

typedef int jmp_buf[2];

on some machine, and:

typedef int jmp_buf[11];

on another. And of course, we all know The Rule about arrays and
pointers in C by now (see http://web.torek.net/torek/c/pa.html),
so:

int *p;
int arr[N];
...
p = arr;

is quite OK (setting p to &arr[0]). If jmp_buf is a typedef for
"array N of int", then -- regardless of the constant N -- the
assignment to "int *" is OK.

Of course, the moment you change compilers and/or machines, you
may find the code is no longer OK, because now perhaps jmp_buf
is a typedef of the form:

typedef struct __jmp_buf {
short __jb_rX;
long __jb_rY;
char *__jb_rZ;
} jmp_buf[1];

So it is not a good idea to use "int *" to point to a jmp_buf,
even if it happens to work on the machine at hand.
--
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.
Nov 14 '05 #5
"JS" <dsa.@asdf.com> writes:
I have this struct:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};
Indentation is your friend. Try it this way:

struct pcb {
void *(*start_routine) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(struct pcb));
That last statement would be better written as:

pcb_pointer = malloc(sizeof *pcb_pointer);

The cast is unnecessary and can mask errors. By applying the sizeof
operator to whatever pcb_pointer points to, it becomes obvious that
you're using the size of the correct type; referring to the size of
the type "struct pcb" means it's correct only if pcb_pointer is of
type struct pcb* (which it is today, but may not be tomorrow).
Now I have this:
state_pointer = (int *) &pcb_pointer->state;
state_pointer point to the address of state in the pcb struct.

What confuses me is that state_pointer is an integer pointer while state has
type jmp_buf. How can an integer pointer point to a variable with type
jmp_buf??


It points to a variable with type jmp_buf because you told it to. The
(int*) cast converts the value (which is of type jmp_buf*) to type
int*.

It's legal to use a cast operator to convert a pointer of one type to
a pointer of another type. Typically, if you know what you're doing,
this can be a way to interpret the pointed-to memory as if it were of
some other type. If you don't know what you're doing, it's a good way
to make your program misbehave.

Why is this code converting a jmp_buf* to an int*? How should we
know? It's your code, not ours.

In this case, I'm guessing that it's a way to dig into the internals
of the jmp_buf by viewing it as an array of int. That's usually a
very bad idea unless you happen to know the details of the
implementation. If the jmp_buf isn't properly aligned, you can even
invoke undefined behavior.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Andrey Tarasevich <an**************@hotmail.com> writes:
[...]
At the same time, C standard guarantees that if 'jmp_buf' is an
aggregate type and the first member of that aggregate type is of type
'int', then the resultant pointer will point to that first member. That
would be portable. Otherwise, you are probably dealing with a hack that
can only be explained by some platform-specific reasons.


The standard guarantees that jmp_buf is an array type. The length and
element type are not specified.

On one system I use, it's a 208-byte array consisting of 52 4-byte
elements. On another, it's a 704-byte array consisting of a single
704-byte element.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

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

Similar topics

20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
42
by: x-pander | last post by:
Is is guaranteed, that a pointer to any object in C points exactly at the lowest addressed byte of this object? Specifcally is it possible for any platform/os/compiler combination that: (char...
12
by: Jason Curl | last post by:
Hello, Just a clarification of the specification about the C-Standard's interpretation of pointers. I know that 'char *' and 'void *' are equivalent and that 'int *' may be different in...
47
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the...
10
by: gmtonyhoyt | last post by:
It's been mentioned to me that, in standard c, that you can have structures behave very much like classes in relation to something to do with function calls within a structure. Now, I didn't get...
7
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false;...
17
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
9
by: mazzawi | last post by:
is it possible to have pointers to bit fields? i read somewhere that you can't. but what if we do this typedef struct u_int3_t { unsigned int u_int3_t:3; } CMI; u_int3_t Array;
16
by: Wabz | last post by:
I am trying to use pointers to create a payroll program. I've used various functions to do the process, but I seem to be having problems in calling the functions appropriately so each pointer knows...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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

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.