Hello, I was hoping a few people would take a quick look at some super simple code I created. The program simply prints the memory location, size, and value of different types of variables. What I don't like about it is that it seems like a bit much. I was thinking of putting the variables in a structure but I don't think that will help too much. I also might make a different function for each print job to help "condense" it. If you think there is a better way to display the variable name and associated information, please let me know, thank you!
#include <iostream>
usingnamespace std;
int main()
{
int int_variable = 1;
bool bool_variable = true;
char char_variable = 'A';
shortint short_variable = 32766;
longint long_variable = 9223372036854775806;
float float_variable = 89234.454;
double double_variable = 308923.4567;
longdouble long_double_variable = 1029384.5938245;
double double_array[5] = {2.5,8.93};
int int_array[4] = {0,1,2,3};
int *ptr_int = nullptr;
float *ptr_float = nullptr;
ptr_int = &int_variable;
ptr_float = &float_variable;
cout << "**********Integer Variable**********" << endl
<< "The address of the int_variable is " << &int_variable << endl
<< "The size of the int_variable is " << sizeof(int_variable) <<
" bytes " << endl
<< "The value in int_variable is " << int_variable << endl << endl;
cout << "**********Bool Variable**********" << endl
<< "The address of the bool_variable is " << &bool_variable << endl
<< "The size of the bool_variable is " << sizeof(bool_variable)
<< " bytes " << endl
<< "The value in bool_variable is " << bool_variable << endl << endl;
cout << "**********Character Variable********** " << endl
<< "The address of the char_variable is " << &char_variable << endl
<< "The size of the char_variable is " << sizeof(char_variable) <<
" bytes " << endl
<< "The value in char_variable is " << char_variable << endl << endl;
cout << "**********Short Variable**********" << endl
<< "The address of the short_variable is " << &short_variable
<< endl
<< "The size of the short_variable is "<< sizeof(short_variable)
<< " bytes " << endl
<< "The value in short_variable is " << short_variable
<< endl << endl;
cout << "*********Float Variable*********" << endl
<< "The address of the float_variable is " << &float_variable
<< endl
<< "The size of the float_variable is " << sizeof(float_variable)
<< " bytes " << endl
<< "The value in float_variable is " << float_variable << endl
<< endl;
cout << "**********Double Variable**********" << endl
<< "The address of the double_variable is " << &double_variable << endl
<< "The size of the double_variable is " << sizeof(double_variable)
<< " bytes " << endl
<< "The value in double_variable is " << double_variable << endl
<< endl;
cout << "**********Long Double Variable**********" << endl
<< "The address of the long_double_variable is "
<< &long_double_variable << endl
<< "The size of the long_double_variable is "
<< sizeof(long_double_variable) << " bytes " << endl
<< "The value in long_double_variable is " << long_double_variable
<< endl << endl;
cout << "**********Double Array**********" << endl
<< "The address of the first element in double_array is "
<< &double_array[0] << endl
<< "The size of the element in the double_array is "
<< sizeof(double_array) << " bytes " << endl
<< "The value of the first element in the double_array is "
<< double_array[0] << endl << endl;
cout << "**********Integer Array**********" << endl;
for (int count = 0; count < 4; count++)
cout << "The address of element " << count << " in int_array is "
<< &int_array[count] << endl
<< "The size of element " << count << " in the int_array is "
<< sizeof(int_array) << " bytes " << endl
<< "The value of element " << count << " in the int_array is "
<< int_array[count] << endl << endl;
cout << "**********Integer Pointer Variable**********" << endl
<< "The address the integer pointer variable points to is "
<< ptr_int << endl
<< "The size of the int_variable is " << sizeof(int_variable)
<<" bytes " << endl
<< "The value in int_variable is " << int_variable << endl << endl;
cout << "**********Float Pointer Variable**********" << endl
<< "The address the float pointer variable points to is "
<< ptr_float << endl
<< "The size of the float_variable is " << sizeof(float_variable)
<<" bytes " << endl
<< "The value in float_variable is " << float_variable << endl << endl;
return 0;
}
Sorry, I didn't get round to sorting the arrays out similarly, but you should get the gist.
If you find yourself writing the same thing lots of times then consider functions ... and if variables of different types then consider templates: http://www.cplusplus.com/doc/tutorial/functions2/