473,396 Members | 1,860 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,396 software developers and data experts.

Void reinterpret cast question

2
Hello, I have got a question, in the following code:

Expand|Select|Wrap|Line Numbers
  1. void func(void* v, int n, int val) {
  2.     int* xp = reinterpret_cast<int*>(&v);
  3.     for(int* i=xp; i<xp+n; i++)
  4.        *i = 5;
  5. }
  6. int main() {
  7.     int array[10];
  8.     func(array, 10, 5);
  9.  
  10.     for (int j=0; j<10; j++)
  11.         cout << array[j] << endl;
  12. }
When passing the array into the function the array values don't change. However, if I just put the function contents directly in main(), I manage to change the values of the array.
Am I doing something wrong?
Aug 22 '08 #1
4 1878
newb16
687 512MB
because this is not original pointer to the beginning of array, but pointer to
v pointer itself ( &v ).
int* xp = reinterpret_cast<int*>(&v);
Aug 22 '08 #2
jfsd
2
Thanks for the hint. Changing the line to this makes it work:
Expand|Select|Wrap|Line Numbers
  1. int* xp = reinterpret_cast<int*>(v);
Aug 22 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
reinterpret_cast is not required.

What is required is understanding how arrays work.

Read this: http://bytes.com/forum/thread772412.html.

I have never seen a case in C++ where you need write a new function with a void* argument.
Aug 22 '08 #4
First, I agree with the above reply that we rarely need to use void pointer as an argument.
However, I still think there are some circumstances that we need it.

Anyway, for this case, why don't you change it to
Expand|Select|Wrap|Line Numbers
  1. void func(int* v, int n, int val) {
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. void func(int v[], int n, int val) {
  2.  
instead, so that you don't have to worry about changing the type.

Another alternative way would be to use "memset" function.
You can find a documentation here

And if you want to use this function for many types of data, I suggest you define it using "template " like this:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. void func(T* v, T n, T val) {
  3.     T* xp = v;
  4.     for(T* i=xp; i<xp+n; i++)
  5.        *i = val;
  6. }
  7.  
*I think you want to put "val" there instead of "5", so I changed it.
Aug 25 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: A | last post by:
Hi, I have not been able to find an example for using the reinterpret cast. Any ideas? Regards, A
26
by: Janice | last post by:
What is the major reason for using void*? When should we use void* for both input arguments and return value? How can we cast the void* pointer to the type we need? Thanx
5
by: WittyGuy | last post by:
How to typecast a "function pointer" to "const void*" type in C++ way? int MyFunction (double money); // Function prototype const void* arg = (const void*)MyFunction; // type casting...
1
by: Muhammad Haseeb | last post by:
plz help me how to use the reinterpret cast operator in filing in c++
12
by: andrew_nuss | last post by:
Hi, I have a template array class on T with specialization for bloat when T is void*. Thus when T is really an int*, my base ptr is physically a void** and needs to be converted to an int**. ...
0
by: NiveditaB06 | last post by:
it is recommended not to use reinterpret cast that is mostly used for type converting one pointer type to other totally different type( like int ).After casting one member variable to another type...
28
by: Peter Olcott | last post by:
I want to make a generic interface between a scripting language and native code, the native code and the interpreter will both be written in C++. The interpreter will probably be implemented as a...
14
by: andreyvul | last post by:
g++ says a reinterpret cast from void* is disallowed; however, all I'm trying to do is de-capsulate a void* argument that holds the pointer to a class (and static_cast forces cast constructor) any...
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:
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
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,...
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...
0
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...

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.