Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
It is correct to cast to int after dereferencing a void pointer?
void* aPtr = NULL;  // we don't yet know what it points to.
...
aPtr = &height;     // it has the address of height, but no type yet.
...
int h = (int)*aPtr; // with casting, we can now go to that address
                    // and fetch an integer value.

(Learn C Programming; Jeff Szuhay)

'with casting, we can now go [...]'

The question is - can we really?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Dereferencing and Casting Void Ptr (Learn C Programming, Jeff Szuhay)

'with casting, we can now go [...]'

The question is - can we really?

Yes, but the shown code doesn't do any dereferencing. Well, it tries to dereference a void* and cast the result to int. That's not how it should be done. You must first cast to int* and then dereference that int*.

int h = *(int*)aPtr;    // now dereferenced ok (assuming `height` is an `int`)
//      ^   ^
//      |   |
//      |  proper cast
//      |
// dereferencing the right thing
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!