473,698 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointers to fields in a struct

JS
I have this struct:

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

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(s truct 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 2336
JS wrote:
I have this struct:

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

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(s truct 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.l earn.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_routin e) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(s truct 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_routin e) (void *);
void *arg;
jmp_buf state;
int stak[1024];
};

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(s truct 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**********@n ews.net.uni-c.dk>, JS <dsa.@asdf.co m> wrote:
I have [a pointer to struct containing a jmp_buf named "state", and also]int *state_pointer; ...state_pointe r = (int *) &pcb_pointer->state;

state_pointe r 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.co m> writes:
I have this struct:

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

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

These pointers:
int *state_pointer;
struct pcb *pcb_pointer;
pcb_pointer = (struct pcb *) malloc(sizeof(s truct 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_Keit h) 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_Keit h) 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
2953
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) struct, I am storing an array of 27 pointers and a void pointer that can point to anything. typedef struct trieNode { struct trieNode *children; // The children nodes void *obj; // The object stored } TrieNode;
42
2157
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 *)v != (void *)v where v is an int variable for example. If so, any real-life examples?
12
1457
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 representation to 'char *' (but most often there is no difference on modern compilers/machines). So if I define a function such as (e.g. often for callbacks)
47
2635
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 rule: if I cast a (sometype **) to a (void **) I am making a number of assumptions about the implementation's (void **) representation and length. Specifically, if I do the above cast I'm assuming that a (sometype **) and a (void **) have the same...
10
8661
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 the details, but does someone have a clue of what this person perhaps was talking about? Do you think he just ment something such as function pointers? Or could I, much like a C++ Class, call a class function that has access to the structure...
7
1883
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; string value3 = "Hello Detroit"; public class x {
17
2568
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
4770
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
328
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 where to read its data from. Here's what I have so far...can someone help me figure out what am doing wrong? Many thanks. #include <ctype.h> #include <stdlib.h>
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.