Jump to content
Larry Ullman's Book Forums

michaelzehe

Members
  • Posts

    11
  • Joined

  • Last visited

Posts posted by michaelzehe

  1. // address.cpp - Script 6.2 modified

    #include <iostream>

    int main() {

        // Create some variables.
        unsigned int a = 2;
        float b = 78.53;
        char c = 'D';
        long d = 1904856026;
        int e = 9002356;
        long f=3;

    std::cout <<"a = "<<a<<std::endl;
    std::cout <<"b = "<<b<<std::endl;
        // Print the addresses.
        std::cout << "The address of a is "
        << (unsigned long) &a << "\n";
        std::cout << "The address of b is "
        << (unsigned long) &b << "\n";
        std::cout << "The address of c is "
        << (unsigned long) &c << "\n";
        std::cout << "The address of d is "
        << (unsigned long) &d << "\n";
        std::cout << "The address of e is "
        << (unsigned long) &e << "\n";
        std::cout << "The address of f is "
        << (unsigned long) &f << "\n";

                const unsigned long I=3;
                long arr = {100.987,200.876,300.765};
                long *A_ptr= arr;
                for (int i =0; i<I; ++i){
                    std::cout<<reinterpret_cast<long>(A_ptr)<<std::endl;
                    A_ptr++;
                }

        std::cout << "Press Enter or Return to continue.\n";
        std::cin.get();

        return 0;

    } // End of the main() function.

     

    This gives the following output:

     

    a=2

    d=1904856026

    The address of a is 229536

    The address of b is 229532

    The address of c is 229531

    The address of d is 229524

    The address of e is 229520

    The address of a is 229516

     

    address for an element of (long) array is 2293504

    address for an element of (long) array is 2293508

    address for an element of (long) array is 2293512

     

    Press Enter or Continue to continue.

     

     

    This output tells me that the long constant 'D' has a length of 7, but when a long loop was built, its addresses are 4 bytes apart!

    The unsigned aspect of the problem doesn't come in here.

     

     

     

     

     

     

     

     

     

     

  2. Hello everyone:

     

    I am confused about the output from script 6.3.

     

    I read it that the integer has a length of 4 bytes, character = 1, float = 4.  But the unsigned long gives a value indicating a length of 7.  At least to me.  Making in a 'non unsigned; long doesn't change things.

     

    Making up an array of unsigned longs and printing out their addresses like in script 6.5 gives lengths of 4 bytes, not 7.  Does anyone understand this?  Thank you.

     

     

     

    MJZ

  3. I was reading another C++ book (!) and i saw that they never used those annoying std:: prefixes for strings etc. and I saw they used a statement at the beginning  that says:  " Using namespace std; "

     

    So it tried it on one of the scripts in this book and it seems to work.  Is there anything wrong with me putting this into the codes so I can study the flow, etc without the std:: prefixes?  Thank you.

×
×
  • Create New...