473,836 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deferencing void pointer

I can't believe I've been trying to work this out for hours now, and I
can't believe I couldn't find someone asking for a similar solution in
the newsgroups. No wonder I hate C so much, and every time I get the
textbooks out end up throwing them against the wall in rage. Thats
been going on for 10 years now.

Anyway, I have:

typedef struct _record {
int age;
} record;

typedef struct _LinkedList {
void *data;
struct _LinkedList *next;
} LinkedList;

I can add items to my linked list, traverse the list etc. However I
CANNOT get the data OUT of the list.

curr_ptr = head;
while ( curr_ptr != NULL ) {
printf ("traverse: this=%p data=%d\n",
curr_ptr, curr_ptr->data->age);
}

This results in the error:
warning: deferencing 'void *' pointer
request for member 'age' in something not a structure or union.

I know I have to cast it or something.... but I just can't figure it
out!
Arrgh!

Doug
Nov 13 '05
52 5672
In <bp**********@t itan.btinternet .com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:
In <bp**********@h ercules.btinter net.com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
Modulo the first == (which should be = instead), yes, I'm sure. (Oops.)
See, it's much better to always triple check than to rely on silly gadgets
that work only when you're lucky!


I don't /rely/ on what you call silly gadgets. I use the const==var order as
an additional check, because I know I make mistakes of this kind, no matter
how carefully I check. I know you do, too, because you have done so in an
article posted to this group, so please don't get all high and mighty on
me.


I did it in a context where it was a syntax error, so there was no risk
of silently getting the wrong result. Big difference. When I know that
the compiler can't help, I do the triple checking. When merely providing
an initialiser, I'm less careful.
In any event, const==var can never catch a mistakenly typed == where = was
meant; its purpose is to catch (where possible) the /opposite/ mistake.


While the purpose of triple checking is to catch *any* mistake related to
the (mis)usage of the assignment/equality operators. Thus rendering the
silly const==var trick futile.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #21
In <7f************ *************@p osting.google.c om> ka****@acm.org (karl malbrain) writes:
It's generally necessary to port C code to C++.


It NEVER happened to me... C++ has an official interface for
interoperating with C code, which completely removes the need to port C
code to C++.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #22
Dan Pop wrote:
In <bp**********@t itan.btinternet .com> Richard Heathfield
.... snip ...
In any event, const==var can never catch a mistakenly typed ==
where = was meant; its purpose is to catch (where possible)
the /opposite/ mistake.


While the purpose of triple checking is to catch *any* mistake
related to the (mis)usage of the assignment/equality operators.
Thus rendering the silly const==var trick futile.


Most of us mere mortals have, in the past, contrived to re-read
our own code many more than three times, without catching such
errors. There appears to be a fault in our infallibility
mechanism. Thus we find a net gain in using the zero effort
"trick" up front.

My favorite foolish foulup is to add an extraneous semi to a macro
definition. At least that one tends to trigger misleading
compiler complaints rather than silent acceptance.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #23
In <3F************ **@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
Dan Pop wrote:
In <bp**********@t itan.btinternet .com> Richard Heathfield
... snip ...
> In any event, const==var can never catch a mistakenly typed ==
> where = was meant; its purpose is to catch (where possible)
> the /opposite/ mistake.


While the purpose of triple checking is to catch *any* mistake
related to the (mis)usage of the assignment/equality operators.
Thus rendering the silly const==var trick futile.


Most of us mere mortals have, in the past, contrived to re-read
our own code many more than three times, without catching such
errors. There appears to be a fault in our infallibility
mechanism.


If you triple check at the time you write it, it is practically impossible
to end up with the wrong operator: "hey, I wrote ==, is it really what I
want?".
Thus we find a net gain in using the zero effort "trick" up front.
Your fondness of zero effort "tricks" probably explains the high rate of
bugs in the code you post... It takes real effort to write correct C
code!
My favorite foolish foulup is to add an extraneous semi to a macro
definition.


Another place where I triple check, because I don't even want to consider
the consequences of a mistake.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #24
Dan Pop wrote:
In <bp**********@t itan.btinternet .com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
I don't /rely/ on what you call silly gadgets. I use the const==var order
as an additional check, because I know I make mistakes of this kind, no
matter how carefully I check. I know you do, too, because you have done so
in an article posted to this group, so please don't get all high and
mighty on me.
I did it in a context where it was a syntax error, so there was no risk
of silently getting the wrong result. Big difference.


No, no real difference. It just shows that we're all fallible - even you.
You have your ways of avoiding errors, and I have mine. Yours don't always
work, and neither do mine. We all make out the best we can.
When I know that
the compiler can't help, I do the triple checking. When merely providing
an initialiser, I'm less careful.


I try to be careful /all/ the time. I don't always succeed. I use all the
useful techniques I know of to assist me in reducing the number of errors
in my code, of which the const==var technique is just one. That you do not
happen to value it does not make it valueless to /me/.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #25
Da*****@cern.ch (Dan Pop) wrote in message news:<bp******* ***@sunnews.cer n.ch>...
In <7f************ *************@p osting.google.c om> ka****@acm.org (karl malbrain) writes:
It's generally necessary to port C code to C++.


It NEVER happened to me... C++ has an official interface for
interoperating with C code, which completely removes the need to port C
code to C++.


Nonsense. Lot's of porting falls completely outside any "official
interface." karl m
Nov 13 '05 #26
In <bp**********@t itan.btinternet .com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:
In <bp**********@t itan.btinternet .com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
I don't /rely/ on what you call silly gadgets. I use the const==var order
as an additional check, because I know I make mistakes of this kind, no
matter how carefully I check. I know you do, too, because you have done so
in an article posted to this group, so please don't get all high and
mighty on me.
I did it in a context where it was a syntax error, so there was no risk
of silently getting the wrong result. Big difference.


No, no real difference.


Even a blind can see the difference between a mistake that requires a
diagnostic one that doesn't.
It just shows that we're all fallible - even you.
Of course I'm fallible and I have provided ample proof of that, over the
years. It's just that I am perfectly able to avoid certain insidious bugs
by paying extra attention at the points where I have a chance to actually
generate them, without resorting to any silly tricks.
I try to be careful /all/ the time. I don't always succeed. I use all the
useful techniques I know of to assist me in reducing the number of errors
in my code, of which the const==var technique is just one. That you do not
happen to value it does not make it valueless to /me/.


Which goes on to show that there is something wrong with your programming
methodology. However, if you're happy with it, keep doing things your
way... Just don't encourage other people to follow your example.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #27
In <7f************ *************@p osting.google.c om> ka****@acm.org (karl malbrain) writes:
Da*****@cern.c h (Dan Pop) wrote in message news:<bp******* ***@sunnews.cer n.ch>...
In <7f************ *************@p osting.google.c om> ka****@acm.org (karl malbrain) writes:
>It's generally necessary to port C code to C++.


It NEVER happened to me... C++ has an official interface for
interoperating with C code, which completely removes the need to port C
code to C++.


Nonsense. Lot's of porting falls completely outside any "official
interface." karl m


???

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #28
Dan Pop <Da*****@cern.c h> wrote:
In <7f************ *************@p osting.google.c om> ka****@acm.org (karl malbrain) writes:
Da*****@cern. ch (Dan Pop) wrote in message news:<bp******* ***@sunnews.cer n.ch>...
In <7f************ *************@p osting.google.c om> ka****@acm.org (karl malbrain) writes:

>It's generally necessary to port C code to C++.

It NEVER happened to me... C++ has an official interface for
interoperating with C code, which completely removes the need to port C
code to C++.


Nonsense. Lot's of porting falls completely outside any "official
interface." karl m

???


Methinks that he thinks that "official interface" implies
"standard library" :-)

Alex
Nov 13 '05 #29
Dan Pop wrote:
In <bp**********@t itan.btinternet .com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:
In <bp**********@t itan.btinternet .com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:

I don't /rely/ on what you call silly gadgets. I use the const==var
order as an additional check, because I know I make mistakes of this
kind, no matter how carefully I check. I know you do, too, because you
have done so in an article posted to this group, so please don't get all
high and mighty on me.

I did it in a context where it was a syntax error, so there was no risk
of silently getting the wrong result. Big difference.
No, no real difference.


Even a blind can see the difference between a mistake that requires a
diagnostic one that doesn't.


Oh, of course I can. That's why I use the const == var construct.
It just shows that we're all fallible - even you.


Of course I'm fallible and I have provided ample proof of that, over the
years. It's just that I am perfectly able to avoid certain insidious bugs
by paying extra attention at the points where I have a chance to actually
generate them, without resorting to any silly tricks.


And I am perfectly able to avoid certain insidious bugs by using a technique
that requires a diagnostic if I type = instead of ==, instead of being
silly enough to rely solely on my remembering to triple-check each time.
One man's silly is another man's sensible. To use words like "silly" in
this context is silly. (There ya go - two sig block quotes for the price of
one.) (Or is that three?)
I try to be careful /all/ the time. I don't always succeed. I use all the
useful techniques I know of to assist me in reducing the number of errors
in my code, of which the const==var technique is just one. That you do not
happen to value it does not make it valueless to /me/.


Which goes on to show that there is something wrong with your programming
methodology.


I disagree.
However, if you're happy with it, keep doing things your
way...
I plan to do just that.
Just don't encourage other people to follow your example.


Oh, but I /do/. You see, it's a sensible technique for fallible people. That
you disagree does not affect my opinion in the slightest, despite the high
regard I have for your knowledge of the C language.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #30

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

Similar topics

15
4172
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: struct node { struct node *next; void *data; };
6
8272
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the generic pointer type. My real question is, is (void *) such a generic pointer type that it
188
17482
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
9
5303
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer from integer of different size. Now I thought that when it was a void I could pass anything? Thing is it works when I use an int, but in this case I wanted to use a char. It wouldnt be hard to work around it, but it annoys me because I've heard...
5
3528
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can store a value in. The actual key argument is always of the same pointer type (as seen in the caller,...
27
8992
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in advance. Erik
49
2825
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code ;-): char *cp; void *vp; void **vpp;
2
2183
by: Siv | last post by:
what is meant by dereferecing pointer? why does it occur? how to fix it?
28
1831
by: junky_fellow | last post by:
Guys, Consider a function func(void **var) { /* In function I need to typecast the variable var as (int **) I mean to say, I need to access var as (int **) }
0
9816
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
10840
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10546
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
10588
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
10254
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
9371
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...
0
6978
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
5647
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...
1
4448
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 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.