473,406 Members | 2,217 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,406 software developers and data experts.

Arrays + reinterpret_cast Help

JavierL
17
I am doing a practice exercise from "thinking in c++" whose answer isn't covered in the annotated solutions guide, so I'm trying to handle it, but I don't understand what I'm doing wrong. Here is the exercise:

28.Create a function that takes a pointer to an array of double and a value indicating the size of that array. The function should print each element in the array. Now create an array of double and initialize each element to zero, then use your function to print the array. Next use reinterpret_cast to cast the starting address of your array to an unsigned char*, and set each byte of the array to 1 (hint: you’ll need to use sizeof to calculate the number of bytes in a double). Now use your array-printing function to print the results. Why do you think each element was not set to the value 1.0?

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3.     int main()
  4.     {
  5.         double p[6]={0,0,0,0,0,0};
  6.         array_passer(p,6);
  7.         return 0;
  8.     }
  9.  
  10. void array_passer(double *p, int size){
  11.  
  12.  
  13.     unsigned char* x = reinterpret_cast<unsigned char*>(&p);
  14.         for(int i = 0; i < sizeof(double)*size; i++){
  15.             *(x+i) = 1;  // With the debugger i get an issue over here but it prints some '1' on the bytes
  16.           //  cout << x[i]<<endl;
  17.         }
  18.     double *z= reinterpret_cast<double*>(&x);
  19.     for(int i =0;i<size;i++){
  20.         cout << z[i] << endl;
  21.     }
  22. }
  23.  
  24.  
Please help :).
Apr 28 '10 #1

✓ answered by newb16

>unsigned char* x = reinterpret_cast<unsigned char*>(&p);

Wrong - you need to reinterpret p, pointer value p contains, not the address of pointer variable itself.
unsigned char* x = reinterpret_cast<unsigned char*>(p);

> double *z= reinterpret_cast<double*>(&x);
Again - here you takes an address of you local variable x, not the value it contains.
Should be double *z= reinterpret_cast<double*>(x);

4 8320
whodgson
542 512MB
Once is enough. e.g. array[6]={0};
to what does size refer and where is it initialized?
Apr 28 '10 #2
JavierL
17
Is the array size it is passed through the function array_passer.
Apr 28 '10 #3
newb16
687 512MB
>unsigned char* x = reinterpret_cast<unsigned char*>(&p);

Wrong - you need to reinterpret p, pointer value p contains, not the address of pointer variable itself.
unsigned char* x = reinterpret_cast<unsigned char*>(p);

> double *z= reinterpret_cast<double*>(&x);
Again - here you takes an address of you local variable x, not the value it contains.
Should be double *z= reinterpret_cast<double*>(x);
Apr 28 '10 #4
Frinavale
9,735 Expert Mod 8TB
I learned something new today :)
For those of us who don't know what reinterpret_cast does (or how to use it) here's the MSDN documentation on the reinterpret_cast Operator (C++).

-Frinny
Apr 29 '10 #5

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

Similar topics

17
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. ...
4
by: k-man | last post by:
Hi! I'll try to make it short: 1) is memset() optimised is some way (I suppose at various level depending of implementations) and if so, to what level? Is it faster to memset() 4kbytes of...
3
by: Bill Reyn | last post by:
I am a Java programmer, a newbie with c++, struggling a bit... Why does the code below return the starting address for an integer array, but for a char array it does not return the starting...
25
by: Jack | last post by:
Hi, Is there a general solution for the following problem: I have an array of instances of class B. Class B is publicly derived from class A. Then I have a class named Buffer that generally...
2
by: Taran | last post by:
Hi All, I was trying some code which essentially does what the function 'func' here does. To simplify the things and make a consise post I have created a dummy app to show my problem. The issue...
7
by: Peter | last post by:
I never used reinterpret_cast -- probably because I don't know what it means. Can somebody enlighten me? I looked into Stroustrup's "The annoted C++ reference manual" -- but this was no help....
27
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong...
2
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
10
by: aaragon | last post by:
Hello everyone, I've been trying to create a simple way to serialize C++ objects so I can send messages among different processors (using MPI). Something very simple is shown below: template...
2
by: ciccio | last post by:
Hi, I was wondering what the main reason is why reinterpret_cast fails to work as expected when using optimizations. Here is the simple example code which fail to give the correct result when...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
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.