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

pointer type casting:---please explain this program

Expand|Select|Wrap|Line Numbers
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <iostream>
  4. using namespace std;
  5. int main(int nNumberofArgs, char* pszArgs[])
  6. {
  7. int upper = 0;
  8. int n = 0;
  9. int lower = 0;
  10. // output the values of the three variables before...
  11. cout << “the initial values are” << endl;
  12. cout << “upper = “ << upper << endl;
  13. cout << “n = “ << n << endl;
  14. cout << “lower = “ << lower << endl;
  15. // now store a double into the space
  16. // allocated for an int
  17. cout << “\nStoring 13.0 into the location &n” << endl;
  18. double* pD = (double*)&n;
  19. *pD = 13.0;
  20. // display the results
  21. cout << “\nThe final results are:” << endl;
  22. cout << “upper = “ << upper << endl;
  23. cout << “n = “ << n << endl;
  24. cout << “lower = “ << lower << endl;
  25. // wait until user is ready before terminating program
  26. // to allow the user to see the program results
  27. system(“PAUSE”);
  28. return 0;
  29. }
Oct 16 '12 #1
3 2067
zmbd
5,501 Expert Mod 4TB
Exactly, what is it that you want us to do?
Oct 16 '12 #2
Rabbit
12,516 Expert Mod 8TB
The program stores a double in the location that was meant to store an integer. In the code, upper and lower is useless as they're not used for anything. And the n doesn't change because the number isn't large enough.
Oct 16 '12 #3
Banfa
9,065 Expert Mod 8TB
What this is is an extremely bad way of examining the memory used by a double. In fairness there are no good ways to do this but there are less bad ways.

This way makes assumptions about how memory (on the stack) works and how integers are represented on the platform and it uses C style casts not C++ casts.

A better way (but still not good because generally there is no good way to directly access the memory of another object) is to declare your memory with double type instead of int type so there are no assumptions about how memory works when accessed to store a double and then to access this memory using a char pointer.

Using char over int is better because of the fundamental differences between the 2 types as specified in the standard. You see a platform is free to define it's own representation of an integer in memory which may mean that not all bits in the integer actually go to make up its value, some of the bits may be used for trap values, that means if you point a int pointer at a random piece of memory, say a double (or copy a double into an int) you have have a bit set in one of the trap values and the data may not represent a valid integer on the platform at all.

However char is different, they standard guarantees that all bits of the char must represent a bit in memory that the bits in the char are contiguous bits in memory and that contiguous chars represent contiguous locations in memory, that is an array of char is an exact representation of the underlying memory.

All that gives something like this

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   double test = 13.0;
  9.  
  10.   cout << "Memory bytes making up the value: " << test << endl;
  11.  
  12.   unsigned char *memptr = reinterpret_cast<unsigned char*>(&test);
  13.  
  14.   for(size_t ix = 0; ix < sizeof(test); ix++)
  15.   {
  16.     cout << setw(2) << setfill('0') << hex << unsigned(memptr[ix]) << " ";
  17.   }
  18.  
  19.   cout << endl << endl;
  20.  
  21.   // To get it as integer values used maths to ensure correct representation in memory
  22.   unsigned upper = 0;
  23.   unsigned lower = 0;
  24.  
  25.   // Note this assumes little endian since I am doing it on a intel processor
  26.   lower = memptr[0] | (memptr[1] << 8) | (memptr[2] << 16) | (memptr[3] << 24);
  27.   upper = memptr[4] | (memptr[5] << 8) | (memptr[6] << 16) | (memptr[7] << 24);
  28.  
  29.   // display the results
  30.   cout << "The final results are:" << endl;
  31.   cout << dec << "upper = " << upper << endl;
  32.   cout << dec << "lower = " << lower << endl;
  33.  
  34.   return 0;
  35. }
  36.  
Note this isn't good, it is just less bad.
Oct 17 '12 #4

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

Similar topics

3
by: Brian Stubblefield | last post by:
Dear clc members, I am rather new to the C programming language. I have a rather large program that I am currently debugging. Currently, the following snippet of code in the c program: ...
15
by: Chris Readle | last post by:
Hi all, Somewhat new to C and I'm getting the following error from my latest code. Here's the warning I'm getting: chris_readle_project3_assignment3.c: In function `main':...
1
by: Josh Wilson | last post by:
Hey gang, So I have my stdin which is user defined file, and am wanting to write it to a temporary file (seems odd I know, but there is a sincere reason), and then use that file as the stdin for...
6
by: PraZ | last post by:
Hi all. Here is a simple code, which when compiled with gcc results in the warning "incompatible pointer type" for arg 1, as expected. But this is just what I want to do, because it makes it...
8
by: Michael | last post by:
Hi all, why do I get a message: warning: passing arg 1 of `collectInput' from incompatible pointer type In 'main' have: char inputString; collectInput(&inputString);
1
by: wanglei0214 | last post by:
I compiles a program in SLOS, but there is a warning i donot know how to remove? here is the framework of the code: typedef struct device_tree { ...... union {
3
by: Frederick Gotham | last post by:
For objects, we have "void*" as the generic pointer type. For instance: enum ParamType { Int, Double }; void Func(void *const p,ParamType const pt) { switch (pt) { case Int: *(int*)p = 42;...
13
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
9
by: cheesiong | last post by:
Can anyone explain the following codes? especially the cast of pointer type. What does it really mean. Thanks. float f1; unsigned long l1; f1=4.5; l1=*(unsigned long*)&f1;
5
by: plazzasele | last post by:
I want to register a new Person by using the set and get method. I am running my code on a program ( DEVc++), and when i compile the code i get this error message: assignment from incompatible...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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?

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.