473,883 Members | 1,566 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 5678
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:
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


???


Porting code fragments from one function to another, and from C to C++
falls outside the C++ "official interface" for interacting with C.
karl m
Dan

Nov 13 '05 #31
In <bp**********@s parta.btinterne t.com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:
Just don't encourage other people to follow your example.


Oh, but I /do/. You see, it's a sensible technique for fallible people.


Nope, it isn't. All people are fallible, and the only technique that
catches any = vs == error that a compiler cannot or doesn't have to catch
is engaging your brain. If you don't engage the brain, the technique
doesn't buy you anything (it just make your code look silly, from a
logical point of view), if you do, you don't need it in the first place.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #32
In <7f************ **************@ posting.google. com> 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:
>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


???


Porting code fragments from one function to another, and from C to C++
falls outside the C++ "official interface" for interacting with C.


No problem, that's why it's called *porting* code fragments and not
*reusing* code fragments.

It's no different from porting code fragments from C to *any* other
programming language. There is *nothing* special about C++.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #33
On Fri, 21 Nov 2003 10:46:35 +0000, Dan Pop wrote:
In <bp**********@s parta.btinterne t.com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:
Just don't encourage other people to follow your example.


Oh, but I /do/. You see, it's a sensible technique for fallible people.


Nope, it isn't. All people are fallible, and the only technique that
catches any = vs == error that a compiler cannot or doesn't have to catch
is engaging your brain. If you don't engage the brain, the technique
doesn't buy you anything (it just make your code look silly, from a
logical point of view), if you do, you don't need it in the first place.


Logical?
How is (0 == X) any less logical than (X == 0)?
Nov 13 '05 #34
Sheldon Simms <sh**********@y ahoo.com> wrote:
On Fri, 21 Nov 2003 10:46:35 +0000, Dan Pop wrote:
In <bp**********@s parta.btinterne t.com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:

Just don't encourage other people to follow your example.

Oh, but I /do/. You see, it's a sensible technique for fallible people.


Nope, it isn't. All people are fallible, and the only technique that
catches any = vs == error that a compiler cannot or doesn't have to catch
is engaging your brain. If you don't engage the brain, the technique
doesn't buy you anything (it just make your code look silly, from a
logical point of view), if you do, you don't need it in the first place.

Logical?
How is (0 == X) any less logical than (X == 0)?


How many times have you said aloud "if 0 is equal to X then ..."?

Alex
Nov 13 '05 #35
In <pa************ *************** @yahoo.com> Sheldon Simms <sh**********@y ahoo.com> writes:
On Fri, 21 Nov 2003 10:46:35 +0000, Dan Pop wrote:
In <bp**********@s parta.btinterne t.com> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:

Just don't encourage other people to follow your example.

Oh, but I /do/. You see, it's a sensible technique for fallible people.


Nope, it isn't. All people are fallible, and the only technique that
catches any = vs == error that a compiler cannot or doesn't have to catch
is engaging your brain. If you don't engage the brain, the technique
doesn't buy you anything (it just make your code look silly, from a
logical point of view), if you do, you don't need it in the first place.


Logical?
How is (0 == X) any less logical than (X == 0)?


I was not talking about formal logic, merely about code that reflects the
programmer's intention (aka readable code).

You want to test whether X is equal to zero, not whether zero is equal to
X. X == 0 *directly* reflects this intention, while 0 == X doesn't.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #36
Dan Pop wrote:
Sheldon Simms <sh**********@y ahoo.com> writes:

.... snip ...

Logical?
How is (0 == X) any less logical than (X == 0)?


I was not talking about formal logic, merely about code that
reflects the programmer's intention (aka readable code).

You want to test whether X is equal to zero, not whether zero
is equal to X. X == 0 *directly* reflects this intention, while
0 == X doesn't.


No, I want to test whether the entity represented by 0 is
interchangeable with the entity represented by X in terms of some
pre-specified set of attributes. Some consider this symettry,
others consider it special relativity, while others consider it
pouring oil upon the flames. :-)

--
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 #37
Alex wrote:
Sheldon Simms <sh**********@y ahoo.com> wrote:
.... snip ...
Logical?
How is (0 == X) any less logical than (X == 0)?


How many times have you said aloud "if 0 is equal to X then ..."?


In languages other than C, how many times have you said:

if (0 <= x <= 3) then ....

--
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 #38
CBFalconer <cb********@yah oo.com> wrote:
Alex wrote:
Sheldon Simms <sh**********@y ahoo.com> wrote:
... snip ...
> Logical?
> How is (0 == X) any less logical than (X == 0)?


How many times have you said aloud "if 0 is equal to X then ..."?

In languages other than C, how many times have you said: if (0 <= x <= 3) then ....


This is an entirely different issue. Your example is traditional
mathematical notation. If this was a feature of C, I /would/ use
it.

A more relevant subsection of your example would be

if (0 <= x)

which, to me, is unreadable.

Alex
Nov 13 '05 #39
Dan Pop wrote:
In <bp**********@s parta.btinterne t.com> Richard Heathfield
<do******@addre ss.co.uk.invali d> writes:
Dan Pop wrote:
Just don't encourage other people to follow your example.
Oh, but I /do/. You see, it's a sensible technique for fallible people.


Nope, it isn't. All people are fallible, and the only technique that
catches any = vs == error that a compiler cannot or doesn't have to catch
is engaging your brain.


Why restrict yourself to techniques that a compiler cannot or doesn't have
to catch? It seems a very silly restriction.
If you don't engage the brain, the technique
doesn't buy you anything (it just make your code look silly, from a
logical point of view),
Logically, a==b and b==a are equivalent. (Duh.)
if you do, you don't need it in the first place.


You seem to see brain engagement as a Boolean phenomenon. I don't believe
it's as simple as that; people /do/ make mistakes even when their brains
/are/ engaged.

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

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

Similar topics

15
4178
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
8275
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
17512
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
9
5307
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
3530
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
9004
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
2827
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
1832
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
9932
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
9777
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
10833
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,...
1
7957
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
5782
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
5979
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
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
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3226
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.