473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Void pointer

int i = 10;
void *j = &i;
How to print out the integer "10" ?

Thanks.
Jan 24 '08 #1
5 2320
howa wrote:
int i = 10;
void *j = &i;
How to print out the integer "10" ?
#include <iostream>

int main() { std::cout << 10 << std::endl; }

Or where you asking something else?

--
Ian Collins.
Jan 24 '08 #2
Alf P. Steinbach wrote:
* howa:
>int i = 10;
void *j = &i;
How to print out the integer "10" ?

#include <iostream>
int main
{
std::cout << "10" << std::endl;
}
Echo?

--
Ian Collins.
Jan 24 '08 #3
On 1$B7n(B24$BF|(B, $B2<8a(B4$B;~(B08$BJ,(B, "Alf P. Steinbach" <al...@start.nowrote:
* howa:
int i = 10;
void *j = &i;
How to print out the integer "10" ?

Technically the answer is to cast the pointer and dereference it,
*static_cast<int*>(j).

However, as a novice you should not use raw pointers at all, much less
void* pointers: what's the problem you're trying to use void* to solve?
How does the following one compare with static_cast?

e.g. cout<< *(int*)j;
Howard
Jan 24 '08 #4
howa wrote:
On 1$B7n(B24$BF|(B, $B2<8a(B4$B;~(B08$BJ,(B, "Alf P. Steinbach" <al...@start.nowrote:
>* howa:
>>int i = 10;
void *j = &i;
How to print out the integer "10" ?
Technically the answer is to cast the pointer and dereference it,
*static_cast<int*>(j).

However, as a novice you should not use raw pointers at all, much less
void* pointers: what's the problem you're trying to use void* to solve?

How does the following one compare with static_cast?

e.g. cout<< *(int*)j;
It's harder to search for in code and more likely to hide errors (the
compiler will reject inappropriate use of static_cast).

--
Ian Collins.
Jan 24 '08 #5
On 1$B7n(B24$BF|(B, $B2<8a(B5$B;~(B44$BJ,(B, Ian Collins <ian-n...@hotmail.comwrote:
howa wrote:
On 1$B7n(B24$BF|(B, $B2<8a(B4$B;~(B08$BJ,(B, "Alf P. Steinbach" <al...@start.nowrote:
* howa:
>int i = 10;
void *j = &i;
How to print out the integer "10" ?
Technically the answer is to cast the pointer and dereference it,
*static_cast<int*>(j).
However, as a novice you should not use raw pointers at all, much less
void* pointers: what's the problem you're trying to use void* to solve?
How does the following one compare with static_cast?
e.g. cout<< *(int*)j;

It's harder to search for in code and more likely to hide errors (the
compiler will reject inappropriate use of static_cast).

--
Ian Collins.- $Bp,i6Ho0zMQJ8;z(B -

- $Bp}<(Ho0zMQJ8;z(B -
Thanks.
Jan 24 '08 #6

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

Similar topics

15
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: ...
6
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...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
9
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...
5
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...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
27
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...
49
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...
28
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.