/********************************************************************/ /* Program Assignment: Assignment 1 */ /* Name: cmo */ /* Date: Oct. 18, 2004 */ /* Descrition: Test alias logic, unsigned chars */ /********************************************************************/ /********************************************************************/ /* Listing Contents: */ /* General comments */ /* Reuse Instructions */ /* Compilation Instructions */ /* Source Code */ /********************************************************************/ /********************************************************************/ /* General comments */ /* Checking to see how to alias char and int arrays for the */ /* variable "packet" */ /********************************************************************/ /********************************************************************/ /* Reuse Instructions: */ /* Limited potential */ /********************************************************************/ /********************************************************************/ /* Compilation Instructions: */ /* This program can be compiled by itself using: */ /* g++ -l testalias -g testalias.cpp. */ /********************************************************************/ using namespace std; #include //for cout statements #include "/home/cs350/term.projects/include.files/external.h" struct external ex; int main() { // Test chars and unsigned chars int tmp; char a; unsigned char b; cout << "testing use of chars for ints" << endl; cout << "enter a small int" << endl; cin >> tmp; a = tmp; cout << "entered value is: " << (int)a << " as char" << endl; cout << "5*(entered value) is " << (int)(5*a) << endl; cout << "enter another small int" << endl; cin >> tmp; b = tmp; cout << "entered value is: " << (int)b << " as unsigned char" << endl; cout << "5*(entered value) is " << (int)(5*b) << endl; // Test alias ideas char *x; char *y; double value = 1; x = (char *) ex.packet; y = (char *) &value; cout << "Testing alias for char and short packet" << endl; cout << " address of packet: " << x << endl; for ( int i=0; i<256; i++ ) ex.packet[i] = i; // Print contents of beginning of packet as 8-bit ints for ( char i=0; i<8; i++ ) { tmp = *(x+i); cout << "i: " << (int)i << " tmp: " << tmp << endl; } // Change contents of packet through alias for ( int i=0; i<8; i++ ) x[i] = 5; // Print contents of packet and x cout << "Ints & chars:" << endl; for ( int i=0; i<10; i++ ) { cout << " x[" << 2*i << "] = " << (int) x[2*i] << endl; cout << " x[" << 2*i+1 << "] = " << (int) x[2*i+1] << endl; cout << "ex.packet[" << i << "] = " << ex.packet[i] << endl; } // Store double in byte positions 3, 4, 5, 6, 7, 8, 9, 10 of packet for ( int i=0; i<8; i++ ) x[i+3] = y[i]; // Print beginning of packet cout << "Changed value of packet:" << endl; for ( int i=0; i<8; i++ ) cout << " ex.packet[" << i << "] = " << ex.packet[i] << endl; }