// file: lecture_10/example.cc // // local include files // #include "example.h" // method: main // // main program starts here // int main(int argc, char** argv) { // declare a character string // char str2[] = "Alex"; fprintf(stdout, "(null terminated)str2 = %s\n", str2); char str[4]; str[0]='A';str[1]='l';str[2]='e';str[3]='x'; fprintf(stdout, "str[0] = %c (%d)\n", str[0], str[0]); fprintf(stdout, "str[1] = %c (%d)\n", str[1], str[1]); fprintf(stdout, "str[2] = %c (%d)\n", str[2], str[2]); fprintf(stdout, "str[3] = %c (%d)\n", str[3], str[3]); fprintf(stdout, "str[4] = %c (%d)\n", str[4], str[4]); // note that argv[1] is a character string // fprintf(stdout, "argv[1] = %s\n", argv[1]); // let's declare a floating-point array // long N = 3; float x[N]; // float x[atoi(argv[2])]; x[0] = 1.0; x[1] = 2.0; x[2] = 3.0; fprintf(stdout, "x[0] = %f (%f)\n", x[0], x[0]); fprintf(stdout, "x[1] = %f (%f)\n", x[1], x[1]); fprintf(stdout, "x[2] = %f (%f)\n", x[2], x[2]); // use a more flexible format for declaring an array // float y[] = {1.0, 2.0, 3.0}; fprintf(stdout, "y[0] = %f (%f)\n", y[0], y[0]); fprintf(stdout, "y[1] = %f (%f)\n", y[1], y[1]); fprintf(stdout, "y[2] = %f (%f)\n", y[2], y[2]); // arrays can be multidimensional // float z[2][9][27][99][35]; z[0][0][0][0][0] = 0.0; z[0][0][0][0][1] = 27.0; z[0][0][0][0][34] = 99.0; fprintf(stdout, "z[0][0][0][0][0] = %f\n", z[0][0][0][0][0]); fprintf(stdout, "z[0][0][0][0][1] = %f\n", z[0][0][0][0][1]); fprintf(stdout, "z[0][0][0][0][34] = %f\n", z[0][0][0][0][34]); // introduce a pointer // fprintf(stdout, "---- pointers ----\n"); float joe = 27.2727; float* ptr = &joe; fprintf(stdout, "joe = %f\n", joe); fprintf(stdout, "the memory location for joe is %p (%u) (%f)\n", ptr, ptr, *ptr); fprintf(stdout, "the memory location for joe is %p (%u) (%ld)\n", ptr, ptr, (long)*ptr); // interpret a float as an int // int* iptr = (int*)ptr; fprintf(stdout, "the memory location for joe is %p (%u) (%ld)\n", iptr, iptr, *iptr); // increment a pointer // float yy[] = {1.0, 2.0, 3.0, 4.0, 5.0}; float* zz = yy; fprintf(stdout, "y[0] = %f\n", *zz); zz++; fprintf(stdout, "y[1] = %f\n", *zz); zz++; fprintf(stdout, "y[2] = %f\n", *zz); // create a multidimensional array // float yyy[2][3]; float* zzz = (float*)yyy; yyy[0][0] = 1.0; yyy[0][1] = 2.0; yyy[0][2] = 3.0; yyy[1][0] = 4.0; yyy[1][1] = 5.0; yyy[1][2] = 6.0; for (long i = 0; i < 2; i++) { for (long j = 0; j < 3; j++) { fprintf(stdout, "%f (%f) ", yyy[i][j], *zzz++); } fprintf(stdout, "\n"); } // explain a pointer to a pointer // fprintf(stdout, "===== argv =====\n"); fprintf(stdout, "argv = %u\n", argv); fprintf(stdout, "argv[0] = %s (%u)\n", argv[0], argv[0]); fprintf(stdout, "argv[1] = %s (%u)\n", argv[1], argv[1]); fprintf(stdout, "argv[2] = %s (%u)\n", argv[2], argv[2]); fprintf(stdout, "=====\n"); fprintf(stdout, "argv[0] = %s\n", argv[0]); fprintf(stdout, "argv[0][0] = %c\n", argv[0][0]); fprintf(stdout, "argv[0][1] = %c\n", argv[0][1]); // we can use pointers with functions // float* myptr = new float[10]; myptr[0] = 27.0; fprintf(stdout, "BEFORE: %f\n", *myptr); bool status = myfunct(myptr); fprintf(stdout, "AFTER: %f\n", *myptr); delete [] myptr; // allocate an array // float* jordan = new float[99]; delete [] jordan; // to review: char's use null-terminated strings // char* aaa = (char*)"Joe"; // allocates 3+null=4 characters char ccc[] = "Joe"; // allocates 3 characters float bbb[] = {1.0, 2.0, 3.0}; // allocates 3 floats = 3x4 = 12 bytes // exit gracefully // exit(0); } bool myfunct(float* x) { *x = 99.0; // changes the value of x in the calling program because it is a pointer return true; }