473,671 Members | 2,168 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 5633
Richard Heathfield wrote:
.... snip ...
Better: if( (new_item == malloc(sizeof *new_item)) == NULL) {

which, as well as being quicker to type, needs less maintenance and is
easier to read.


Harumph. Are you sure? :-)

--
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 #11
On 2003-11-17, Floyd Davidson <fl***@barrow.c om> wrote:
James Hu <jx*@despammed. com> wrote:
On 2003-11-17, Douglas Garstang <do**@pobox.com > wrote:

... Unfortunately after reading your post several times, I still just
don't get it. I can't even visualise pointers to anything in my head,
let alone pointers to pointers.


...
A pointer to a pointer is analagous to a phone number to a voice
mailbox which can only hold a message that is another phone number.


Ahem... a _pointer_ _to_ _a_ _pointer_ is the phone number of directory
assistance.
...


No, that is a pointer to an oracle, for which there is no C analog, so
it is off-topic in comp.lang.c. Please try rec.humor.oracl e.

-- James
Nov 13 '05 #12
CBFalconer wrote:
Richard Heathfield wrote:

... snip ...

Better: if( (new_item == malloc(sizeof *new_item)) == NULL) {

which, as well as being quicker to type, needs less maintenance and is
easier to read.


Harumph. Are you sure? :-)


Modulo the first == (which should be = instead), yes, I'm sure. (Oops.)

--
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 #13
In <bp**********@h ercules.btinter net.com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
CBFalconer wrote:
Richard Heathfield wrote:

... snip ...

Better: if( (new_item == malloc(sizeof *new_item)) == NULL) {

which, as well as being quicker to type, needs less maintenance and is
easier to read.


Harumph. Are you sure? :-)


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!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14
Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:
I just use new, rather than new_item (mainly for the apoplexy it causes in
C++ programmers).


Well, whatever flaws may result from doing so are well worth causing
apoplexy in C++ programmers, eh? ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #15
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.

In any event, const==var can never catch a mistakenly typed == where = was
meant; its purpose is to catch (where possible) the /opposite/ mistake.

--
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 #16
Christopher Benson-Manica wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> spoke thus:
I just use new, rather than new_item (mainly for the apoplexy it causes
in C++ programmers).


Well, whatever flaws may result from doing so are well worth causing
apoplexy in C++ programmers, eh? ;)


Er, flaws? I haven't spotted any yet. Do feel free to share them. :-)

--
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 #17
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bp******* ***@titan.btint ernet.com>...
Douglas Garstang wrote:
Thanks Richard, well I felt better for literally 5 minutes. Now I've
reached my next stumbling block. I'm trying to make the list functions
generic. I did have something like this in my append function:

int list_append ( LinkedList **list, void *data ) {

LinkedList *new_item;


I just use new, rather than new_item (mainly for the apoplexy it causes in
C++ programmers).


Bzzzt. Not around here you don't! and I'm not even a C++ programmer.
It's generally necessary to port C code to C++.

karl m
Nov 13 '05 #18
On Tue, 18 Nov 2003 16:35:22 -0800, karl malbrain wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message news:<bp******* ***@titan.btint ernet.com>...
Douglas Garstang wrote:
> Thanks Richard, well I felt better for literally 5 minutes. Now I've
> reached my next stumbling block. I'm trying to make the list functions
> generic. I did have something like this in my append function:
>
> int list_append ( LinkedList **list, void *data ) {
>
> LinkedList *new_item;


I just use new, rather than new_item (mainly for the apoplexy it causes in
C++ programmers).


Bzzzt. Not around here you don't! and I'm not even a C++ programmer.
It's generally necessary to port C code to C++.


Why? Did the C compiler stop working?

Nov 13 '05 #19
karl malbrain wrote:
Richard Heathfield <do******@addre ss.co.uk.invali d> wrote in message
news:<bp******* ***@titan.btint ernet.com>...
Douglas Garstang wrote:
>
> LinkedList *new_item;
I just use new, rather than new_item (mainly for the apoplexy it causes
in C++ programmers).


Bzzzt. Not around here you don't!


I see no reason why not.
and I'm not even a C++ programmer.
Irrelevant.
It's generally necessary to port C code to C++.


That has not been my experience. If it ain't broke, don't fix it.

--
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 #20

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

Similar topics

15
4151
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
8227
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
17321
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
9
5289
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
3514
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
8951
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
2776
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
2175
by: Siv | last post by:
what is meant by dereferecing pointer? why does it occur? how to fix it?
28
1807
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
8476
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
8914
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
8820
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
8598
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
8670
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
4224
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
4406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2810
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
1809
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.