DIR Return Create A Forum - Home
---------------------------------------------------------
Essentials of Scientific Computing 2013
HTML https://esc2013.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: Programming Questions
*****************************************************
#Post#: 9--------------------------------------------------
Datatypes Pointers and References
DIR By: mathnathan
Date: July 12, 2013, 5:13 pm
---------------------------------------------------------
Here is the code we looked over together in class
types.cpp
--- Code ---
#include <stdio.h>
#include <climits>
#include <cfloat>
int main() {
printf( "\nSize of basic types on this machine\n" );
printf( "\tsizeof(int) = %lu bytes\n", sizeof(int));
printf( "\tsizeof(double) = %lu bytes\n", sizeof(double));
printf( "\tsizeof(float) = %lu bytes\n", sizeof(float));
printf( "\tsizeof(char) = %lu bytes\n", sizeof(char));
printf( "\tsizeof(bool) = %lu bytes\n", sizeof(bool));
printf( "\tsizeof(long) = %lu bytes\n", sizeof(long));
printf( "\tsizeof(short) = %lu bytes\n", sizeof(short));
printf( "\tsizeof(long long) = %lu bytes\n", sizeof(long
long));
printf("----------------------------------\n");
printf( "\nThis Machine's limits\n" );
printf( "\tNumber of bits in a char object (byte) = %d\n",
CHAR_BIT );
printf( "\tMinimum value for an object of type char =
%d\n", CHAR_MIN );
printf( "\tMaximum value for an object of type char =
%d\n", CHAR_MAX );
printf( "\tMaximum value for an object of type unsigned
char = %u\n", UCHAR_MAX );
printf( "\tMinimum value for an object of type short =
%d\n", SHRT_MIN );
printf( "\tMaximum value for an object of type short =
%d\n", SHRT_MAX );
printf( "\tMaximum value for an object of type unsigned
short = %u\n", USHRT_MAX );
printf( "\tMinimum value for an object of type int =
%d\n", INT_MIN );
printf( "\tMaximum value for an object of type int =
%d\n", INT_MAX );
printf( "\tMaximum value for an object of type unsigned
int = %u\n", UINT_MAX );
printf( "\tMinimum value for an object of type long =
%ld\n", LONG_MIN );
printf( "\tMaximum value for an object of type long =
%ld\n", LONG_MAX );
printf( "\tMaximum value for an object of type unsigned
long = %lu\n", ULONG_MAX );
printf( "\tMinimum value for an object of type long long =
%lld\n", LLONG_MIN );
printf( "\tMaximum value for an object of type long long =
%lld\n", LLONG_MAX );
printf( "\tMaximum value for an object of type unsigned
long long = %llu\n", ULLONG_MAX );
printf( "\tMinimum finite representable floating-point
number = %f\n", FLT_MIN );
printf( "\tMaximum finite representable floating-point
number = %f\n", FLT_MAX );
printf( "\tNumber of decimal digits that can be rounded
into a floating-point and back without change in the number of
decimal digits = %d\n", FLT_DIG );
printf( "\tMinimum finite representable double number =
%f\n", DBL_MIN );
printf( "\tMaximum finite representable double number =
%f\n", DBL_MAX );
printf( "\tNumber of decimal digits that can be rounded
into a double and back without change in the number of decimal
digits = %d\n", DBL_DIG );
return 0;
}
--- End Code ---
refs.cpp
--- Code ---
#include <stdio.h>
#include <vector>
int add1ref( int& a ) {
return a += 1;
}
int add1copy( int a ) {
return a += 1;
}
int* add1ptr( int* a ) {
return a += 1;
}
int main() {
int a = 1;
printf( "a = %d\n", a);
printf( "&a = %p\n", &a);
int& aref = a;
printf( "aref = %x\n", aref);
printf( "&aref = %p\n", &aref);
int* aptr = &a;
printf( "aptr = %p\n", aptr);
printf( "&aptr = %p\n", &aptr);
printf("----------------------------------\n");
printf("After adding 1 to a\n");
a += 1;
printf( "\ta = %d\n", a);
printf( "\t&a = %p\n", &a);
printf( "\taref = %x\n", aref);
printf( "\t&aref = %p\n", &aref);
printf( "\taptr = %p\n", aptr);
printf( "\t&aptr = %p\n", &aptr);
printf("After adding 1 to aref\n");
aref += 1;
printf( "\ta = %d\n", a);
printf( "\t&a = %p\n", &a);
printf( "\taref = %x\n", aref);
printf( "\t&aref = %p\n", &aref);
printf( "\taptr = %p\n", aptr);
printf( "\t&aptr = %p\n", &aptr);
int b = 15;
int* c = &b;
std::vector<int> intVec;
return 0;
}
--- End Code ---
*****************************************************
Page 1 of 1