// file: lecture_20/myprog.cc // // local include files // #include #include #include "myprog.h" // main: myprog // // This is a driver program that does some very simple things. // int main(int argc, const char** argv) { // declare an instance of a the structure "joe" // struct joe my_joe; JOE my_joe2; // assigning data // my_joe.x = 27.0; my_joe.a = 19; my_joe.c = 'a'; my_joe.y = 99.0; my_joe.z = -1.0; my_joe.c2 = 'b'; my_joe2.x = -99.0; my_joe2.y = -27.0; fprintf(stdout, "the value of my_joe.x is %f (%f)\n", my_joe.x, my_joe.y); // do something similar with shoe // struct shoe shoe1; MYSH shoe2; shoe1.foo = 'a'; shoe1.y.x = 27.0; fprintf(stdout, "memory analysis:"); fprintf(stdout, "the struct (my_joe) is at %u\n", &my_joe); fprintf(stdout, "the struct (my_joe2) is at %u\n", &my_joe2); fprintf(stdout, "x is at %u\n", &my_joe.x); fprintf(stdout, "a is at %u\n", &my_joe.a); fprintf(stdout, "c is at %u\n", &my_joe.c); fprintf(stdout, "y is at %u\n", &my_joe.y); fprintf(stdout, "z is at %u\n", &my_joe.z); fprintf(stdout, "c2 is at %u\n", &my_joe.c2); /* fprintf(stdout, "x is at %u\n", &my_joe2.x); fprintf(stdout, "the sizeof the struct is at %u\n", sizeof(my_joe)); */ // assign data as a pointer // JOE* foo = new JOE; JOE* mary = new JOE; foo->x = 99.0; foo->y = 27.0; fprintf(stdout, "the values are (my_joe: %f) (foo: %f)\n", my_joe.x, foo->x); // let's shoe shop // MYSH myshoe; myshoe.brand = (char*)"Nike"; myshoe.size = (float)9.5; setsize(&myshoe); fprintf(stdout, "brand: %s\n", myshoe.brand); fprintf(stdout, "size: %f\n", myshoe.size); /* // memory packing // //fprintf(stdout, "my shoe is located at %u\n", &myshoe); //fprintf(stdout, "my shoe.size is located at %u\n", &myshoe.size); //fprintf(stdout, "my shoe.width is located at %u\n", &myshoe.width); // create an array of shoes // MYSH lots_of_shoes[99]; lots_of_shoes[3].size = -2727.0; fprintf(stdout, "shoe no. 3: size = %f\n", lots_of_shoes[3].size); MYSH* jordan = new MYSH; jordan->size = -9999.0; jordan->brand = new char[99]; strcpy(jordan->brand, (char*)"this is a test"); fprintf(stdout, "jordan->.size = %f\n", jordan->size); fprintf(stdout, "jordan->brand = %s\n", jordan->brand); MYSH* alex[3]; alex[0] = new MYSH; alex[1] = new MYSH; alex[2] = new MYSH; alex[3] = new MYSH; */ // exit gracefully // return(0); } // implement the print function // void setsize(shoe* shoe_a) { shoe_a->size = -2727.0; return; }