473,795 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

((node*)&head)->next

Given that next is the first field in struct node,
and head is a pointer to node,
does assigning ((node*)&head)->next safely assign head ?

Illustration (this code works on many platforms)
#include <stdlib.h>
#include <stdio.h>

/* node structure for linked list */
typedef struct node
{
struct node* next;
char payload;
} node;

int main(void)
{
node *head, *last, *temp;
int c;
/* build list from stdin */
last = (node*)&head;
while ((c = getchar())!=EOF )
{
if ((temp = malloc(sizeof(n ode)))==NULL)
return 1;
temp->payload = c;
last->next = temp;
last = temp;
}
last->next = NULL;
/* then output list */
temp = head;
while (temp!=NULL)
{
putchar(temp->payload);
temp = temp->next;
}
return 0;
}

--
François Grieu
Nov 14 '05 #1
3 2638
In article <fg************ **************@ individual.net>
Francois Grieu <fg****@francen et.fr> wrote:
Given that next is the first field in struct node,
and head is a pointer to node,
does assigning ((node*)&head)->next safely assign head ?

Illustration (this code works on many platforms)
I would be surprised to find a platform on which this does *not*
work:
#include <stdlib.h>
#include <stdio.h>

/* node structure for linked list */
typedef struct node
{
struct node* next;
char payload;
} node;

int main(void)
{
node *head, *last, *temp;
int c;
/* build list from stdin */
last = (node*)&head;
while ((c = getchar())!=EOF )
{
if ((temp = malloc(sizeof(n ode)))==NULL)
return 1;
temp->payload = c;
last->next = temp;
last = temp;
}
last->next = NULL;

[etc]

But there is no need to resort to such subterfuge. Simply use
pointers the way they are intended:

node *head, *temp;
node **npp = &head;

while ((c = getchar()) != EOF) {
temp = malloc(sizeof *temp);
if (temp == NULL)
return EXIT_FAILURE;
temp->payload = c;
*npp = temp;
npp = &temp->next;
}
*npp = NULL;

While the head is just a pointer (of type "struct node *", aka
"node *"), each node also contains a pointer of that same type.
If we point a separate pointer ("npp") at each pointer in turn, we
can set each pointed-to pointer with absolute 100% guaranteed
safety, even though the "head" pointer is *just* a pointer, and
the other pointers are elements of a larger collection.

Naturally, to point to "struct node *" objects, we need a pointer
of type "struct node **" (aka "node **").

Note that this code continues to work even if the "next" field is
moved elsewhere in the list structure -- while the original code,
with its cast, stops working.
--
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 #2
On Mon, 13 Dec 2004 08:53:27 +0100, Francois Grieu wrote:
Given that next is the first field in struct node,
and head is a pointer to node,
does assigning ((node*)&head)->next safely assign head ?
This isn't safe because the alignment of a node structure may be stricter
than the alignment of a pointer to node. Also you are creating a pointer
to node value which isn't null and doesn't point at a valid node object,
or 1 past the end of an array of nodes.
Illustration (this code works on many platforms)


Yes, it is likely to work on many platforms but as far as C is concerned
it invokes undefined behaviour.

Chris's solution is well defined hence better.

Lawrence

Nov 14 '05 #3
Chris Torek <no****@torek.n et> wrote:
Simply use pointers the way they are intended

node *head, *temp;
node **npp = &head;

while ((c = getchar()) != EOF) {
temp = malloc(sizeof *temp);
if (temp == NULL)
return EXIT_FAILURE;
temp->payload = c;
*npp = temp;
npp = &temp->next;
}
*npp = NULL;


Yes, that's clean.

For reasons not apparent in the sample code that I posted,
I wanted to keep a pointer to the last node in the list,
and Chris's code does not do that. But now I realize
that a pointer to the "next" field in the last node in the
list will do just as well, if not better.

Thanks Chris. Also, thanks Lawrence for a concrete reason
why the original code could fail (beside plain non-conformance).

--
François Grieu
Nov 14 '05 #4

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

Similar topics

72
5446
by: Mel | last post by:
Are we going backwards ? (please excuse my spelling...) In my opinion an absolute YES ! Take a look at what we are doing ! we create TAGS, things like <H1> etc. and although there are tools (dreamweaver and the like), they are all at the lowest level of programming (something like assembly as oposed to C++ etc.). These tools create "brain-dead" developers that constantly have to plough through tons of tags to do the simplest thing. ...
0
1354
by: ooze | last post by:
typedef unsigned long PARAM; typedef PARAM SAP; typedef struct qnode { struct qnode *next; struct qnode *prev; } QNODE;
0
5577
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
16
3401
by: drwyness | last post by:
Hello, Please help with the following problem it is causing me some headaches. The following javascript code is designed to fill in text boxes with numbers on an online game (tribalwars). It works fine for everyone else and even the link to the script that I am personally using has been used on other people computers successfully. I have tried this on both IE7 and the latest Firefox. My options show javascript is enabled and even other...
0
9672
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, 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...
0
9519
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,...
1
10163
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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
6780
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
5436
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.