Casting from pointer to int

Posted on 2000-04-04
Medium Priority
Last Modified: 2010-04-02
This code works:
  if ((int) pNode()->pUserField() == 2) .....

but am not sure how to do the opposite ie. plug a value back into the object pNode.   I have tried

  pNode()->pUserField() = (void *) 2;

but this doesn't compile.  How do I set the pUserField to an integer value?

Question by:abulka
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 2686170
Short answer?  Use another function (not pUserField()).  i.e., pUserField() is a function that, if the second line you show doesn't compile, does not return something that can show up on the left hand side of an assignment operator (an "l-value").  Maybe you can rewrite the function pUserField() (and note, it is a function, not a variable, as the name would lead you to believe) to return by reference.  Then you'd be all set!

Author Comment

ID: 2686199
I am using a 3rd party library and cannot rewrite or add new functions.  It should be possible to do with the existing function.  Here is more info on the UserField property:

// This method sets a void pointer to a user field. This pointer is
// originally initialized to NULL.

void * TSDigraph::pUserField(void * pUserField);

// This method gets a void pointer to a user field. This pointer is
// originally initialized to NULL.

void * TSDigraph::pUserField();


Accepted Solution

GlennDean earned 20 total points
ID: 2686239
Instead of writing
pNode()->pUserField() = (void *) 2;
pNode()->pUserField((void *)2);

Expert Comment

ID: 2686254
I work for Tom Sawyer and I can tell you that Glenn is definitely right. He fully deserves the enormous 5 point reward :)

Author Comment

ID: 2686327
Seems to work.  Much appreciated!  Thanks to mnewton, too.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question