Hi,
This is a question asked to me in an interview.
I haven't been able to figure out an answer for
it. Please try to see what can be done about the
following problem.
/* I have been given two structures */
struct node
{
struct node *next;
} *temp;
struct data
{
int a;
float b;
char c;
struct node s;
};
/* Now the question is to access the values of a,b,c using temp */
Thanks in advance. 16 1938
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
Shwetabh <sh**********@g mail.com> wrote: This is a question asked to me in an interview. I haven't been able to figure out an answer for it.
/* I have been given two structures */ struct node { struct node *next; } *temp;
struct data { int a; float b; char c; struct node s; };
/* Now the question is to access the values of a,b,c using temp */
Is it possible that struct node is defined slightly differently, as
struct node
{
struct data *next;
} *temp;
If not then it can't be done without some kind of cheating, as
clearly anything accesses by temp will be a pointer.
There is, of course, ready cheating, such as creating a context in
which temp is a pointer to a struct data instead of a struct node.
--
Oh, to be a Blobel!
Shwetabh wrote: Hi, This is a question asked to me in an interview. I haven't been able to figure out an answer for it. Please try to see what can be done about the following problem.
/* I have been given two structures */ struct node { struct node *next; } *temp;
struct data { int a; float b; char c; struct node s; };
/* Now the question is to access the values of a,b,c using temp */
So you are saying that "temp" is a pointer into data at member "s"?
There is no portable solution, as there is no way to determine the
amount of padding used for structure members. You could guess at it by
doing:
void *v = temp;
v -= sizeof(char) + sizeof(float) + sizeof(int);
struct data *d = v;
But that would only work if the structs had no padding to them, which is
an invalid assumption.
Perhaps another possibility is to do:
struct data d;
void *dp = &d;
void *s = &d.s;
int diff = s - dp;
void *v = tmp;
v -= diff;
struct data *answer = v;
Even then you probably have some pretty platform-specific problems.
Jon
----
Learn to program using Linux assembly language http://www.cafeshops.com/bartlettpublish.8640017
Jonathan Bartlett <jo*****@eskimo .com> wrote: Shwetabh wrote: struct node { struct node *next; } *temp;
struct data { int a; float b; char c; struct node s; };
/* Now the question is to access the values of a,b,c using temp */ So you are saying that "temp" is a pointer into data at member "s"?
No, he isn't saying that. He probably _meant_ that, since without some
hint of that ilk the puzzle is unsolvable, but he didn't say it.
There is no portable solution, as there is no way to determine the amount of padding used for structure members.
Yes, there is, for a known, declared structure: offsetof.
(No, I'm not giving any more hints.)
Richard
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
Shwetabh <sh**********@g mail.com> wrote: Hi, This is a question asked to me in an interview. I haven't been able to figure out an answer for it. Please try to see what can be done about the following problem.
/* I have been given two structures */ struct node { struct node *next; } *temp;
struct data { int a; float b; char c; struct node s; };
/* Now the question is to access the values of a,b,c using temp */
Thanks in advance.
Ick.
The first thing I'd say is "I hope the code at this company doesn't do
silly things like this."
The second thing is that I'd use the macro "offsetof" along with some creative
casting and pointer arithmetic to transform a "struct node *" into
a "stuct data *". The rest is trivial.
But once again, I'd want an assurance that the above silliness isn't standard
practice at the company you're applying for a job at.
Shwetabh wrote: Hi, This is a question asked to me in an interview. I haven't been able to figure out an answer for it. Please try to see what can be done about the following problem.
/* I have been given two structures */ struct node { struct node *next; } *temp;
struct data { int a; float b; char c; struct node s; };
/* Now the question is to access the values of a,b,c using temp */
Thanks in advance.
/* BEGIN new.c */
#include <stdio.h>
int main(void)
{
struct node {
struct node *next;
} *temp;
struct data {
int a;
float b;
char c;
struct node s;
};
struct data ds;
ds.a = 12345;
ds.b = 3.14f;
ds.c = 'X';
temp = (struct node *)&ds;
printf("%d\n%f\ n%c\n",
((struct data *)temp) -> a,
((struct data *)temp) -> b,
((struct data *)temp) -> c
);
return 0;
}
/* END new.c */
N869
6.2.5 Types
[#27] All pointers to structure types
shall have the same representation and alignment
requirements as each other.
6.3.2.3 Pointers
[#7] A pointer to an object or incomplete type may be
converted to a pointer to a different object or incomplete
type. If the resulting pointer is not correctly aligned
for the pointed-to type, the behavior is undefined.
Otherwise, when converted back again, the result shall
compare equal to the original pointer.
--
pete
Shwetabh wrote: Hi, This is a question asked to me in an interview. I haven't been able to figure out an answer for it. Please try to see what can be done about the following problem.
/* I have been given two structures */ struct node { struct node *next; } *temp;
struct data { int a; float b; char c; struct node s; };
/* Now the question is to access the values of a,b,c using temp */
Thanks in advance.
Hmm,it is easy.ANSI C defines a macro 'offsetof' in stddef.h.
/*GCC 3.2.3*/
#ifndef __cplusplus
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#else /* C++ */
/* The reference cast is necessary to thwart an operator& that might
 be applicable to MEMBER's type.See DR 273 for details.*/
#define offsetof(TYPE, MEMBER) (reinterpret_ca st <size_t> \
(&reinter pret_cast <char &>(static_ca st <TYPE *> (0)->MEMBER)))
#endif /* C++ */
On Mon, 18 Jul 2005 14:00:00 +0000, John Cochran wrote: In article <11************ **********@g43g 2000cwa.googleg roups.com>, Shwetabh <sh**********@g mail.com> wrote:Hi, This is a question asked to me in an interview. I haven't been able to figure out an answer for it. Please try to see what can be done about the following problem.
/* I have been given two structures */ struct node { struct node *next; } *temp;
struct data { int a; float b; char c; struct node s; };
/* Now the question is to access the values of a,b,c using temp */
Thanks in advance.
Ick. The first thing I'd say is "I hope the code at this company doesn't do silly things like this."
Why not? This is the way linked lists are implemented in the Linux
kernel. It's a good approach - embed the node in the data rather than
the other way around and hence do away with the need for a data
pointer. And it's portable.
"Netocrat" <ne******@dodo. com.au> wrote: On Mon, 18 Jul 2005 14:00:00 +0000, John Cochran wrote:
In article <11************ **********@g43g 2000cwa.googleg roups.com>, Shwetabh <sh**********@g mail.com> wrote:struct node { struct node *next; } *temp;
struct data { int a; float b; char c; struct node s; };
Ick. The first thing I'd say is "I hope the code at this company doesn't do silly things like this."
Why not? This is the way linked lists are implemented in the Linux kernel. It's a good approach - embed the node in the data rather than the other way around and hence do away with the need for a data pointer. And it's portable.
Myeah - but it would be clearer to have the node first, rather than
last. That way, when you've traversed the list (or, for that matter,
tree, or whatever structure; this trick works with more than lists), you
can simply do dataptr=(struct data *)nodeptr, and avoid fiddling with
offsetof or worse. That cast must work correctly, since a struct must
have the same address as its first member.
Richard
On Tue, 19 Jul 2005 09:50:51 +0000, Richard Bos wrote: "Netocrat" <ne******@dodo. com.au> wrote:
On Mon, 18 Jul 2005 14:00:00 +0000, John Cochran wrote:
> In article <11************ **********@g43g 2000cwa.googleg roups.com>, > Shwetabh <sh**********@g mail.com> wrote: >>struct node >>{ >> struct node *next; >>} *temp; >> >>struct data >>{ >> int a; >> float b; >> char c; >> struct node s; >>}; > > Ick. > The first thing I'd say is "I hope the code at this company doesn't do > silly things like this."
Why not? This is the way linked lists are implemented in the Linux kernel. It's a good approach - embed the node in the data rather than the other way around and hence do away with the need for a data pointer. And it's portable.
Myeah - but it would be clearer to have the node first, rather than last. That way, when you've traversed the list (or, for that matter, tree, or whatever structure; this trick works with more than lists), you can simply do dataptr=(struct data *)nodeptr, and avoid fiddling with offsetof or worse. That cast must work correctly, since a struct must have the same address as its first member.
Totally agree when the data can only be a member of one list. But often
(as is the case in the linux kernel), objects are members of multiple
lists. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Murat Tasan |
last post by:
i'm having trouble with some links using javadoc...
basically, here is the example:
{@link List}.
{@link List#set(int, Object) List.set}.
i'll run javadoc, but only the first link shows up.
i know i have imported the correct packages (in this case, only
java.util.*)...
|
by: VIJAY KUMAR |
last post by:
Hi pals,
I am using 2 web forms (pages). In first page, i have Datagrid control
and on second page i have a hyper link control to the first page and Add
value to the data grid/Database.
Problem:
when I click on the hyperlink on the second page, i am getting the first
page with cahed data. But i didn't declare/Use code for cache data. when i
|
by: Shawn Windle |
last post by:
----begin node.h--------
#ifndef NODE_H
#define NODE_H
#include <iostream> //NULL
using namespace std;
class node
{
|
by: Mario |
last post by:
I reinstalled IE 6 with all the windows 98 updates and patches and the
problem remains exactly. Please tell me how can I solve this ?
> Hello,
>
> Under windows98se and IE 6.0.2800.1106 , few weeks ago all seamed to
> work fine.
>
> Now only in some website if I try to open an internal link to a web
> site, some pages will just start to open and remain blank it wont load
|
by: sudhirlko2001 |
last post by:
How to swap two nodes of doubly Linklist
| |
by: Steve McLellan |
last post by:
Hi,
Sorry to repost, but this is becoming aggravating, and causing me a lot of
wasted time. I've got a reasonably large mixed C++ project, and after a
number of builds (but not a constant number) linking (and sometimes
compiling) becomes immensely slow, and task manager shows that link.exe (or
cl.exe) is barely using any processor time, but an awful lot of RAM (around
150-200MB). I'm going to keep an eye on page faults since I can't...
|
by: free2cric |
last post by:
Hi,
I have a single link list which is sorted.
structure of which is like
typedef struct mylist
{
int num;
struct mylist *next;
|
by: TheRomance |
last post by:
i have a problem about insert integer to link list.
sorry it's too long but i try many times, many ways , it's still have an error
function is fix . can't change anything about function. i can only make a code that work with function.
Number.cpp
#include "Number.h"
|
by: maruf.syfullah |
last post by:
Consider the following Class definitions:
class AClass
{
int ai1;
int ai2;
public:
CClass* c;
AClass(){}
|
by: Casimir Pohjanraito |
last post by:
I have a list of links, with a thumbnail image hidden(resized) next to
the link. Complete html&css at end of this post.
CSS for the link resizes the image on a:hover. All is good,
except the list resizes to accomodate the image, thus "tearing up" the
list. In some browsers the thumbnail and resize effect goes back and forth
real fast (try mouseovering the "another page" link).
I tried constraining the list item height, but that doesnt...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |