Co, Stoker, Walker, and Zhang
Attorneys at Law
March 2000

# Code Examples

(1)

union number {

char a;

int b;

} ;

int main()

{

union number test;

test.b = 1000;

printf("This is the value of test.b: %d <-\n", test.b);

printf("What will test.a evaluate to? : %c <-\n", test.a);

test.a = 'A';

printf("test.a has value : %c <--\n", test.a);

printf("test.b now has value: %d <--\n", test.b);

return 0;

}

/****** PROGRAM OUTPUT ******/

Union test program:

Setting test.b to 1000:

This is the value of test.b: 1000 <-

What will test.a evaluate to? : <-

now assigning test.a to 'A'

test.a has value : A <--

test.b now has value: 1090520040 <--

End of Union test program

/***** end of output *****/

(2)

#include <stdio.h>

#include "bigint.h"

int main()

{

BigInt x, value; // The bigint class is good for holding very large numbers, like 100!

BigInt *a;

a = new BigInt[100];

value = 1;

// generate factorials to store in array

for (x=1; x<=100; x++) {

value = value * x;

a[x-1] = value;

}

for (x=1; x<=100; x++)

printf("%s factorial is %s\n", x, a[x-1]);

delete a;

return 0;

}

(3)

class MyClass {

public:

virtual float GetValue() = 0;

};

class MyClass {

public:

virtual float GetValue() = 10;

};

gcc gives an error:

invalid initializer for virtual method `MyClass::GetValue()'

(4)

main(){

int a;

int* pi;

char* pc;

pi = &a;

pc = (char*) pi; // aliasing

*pi = 117;

*pc = 'a';

printf("pc: %c, pi: %d\n", *pc, *pi);

}