// file: lecture_19/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* str = (char*)"Alex"; // char *str = (char*)"Alex"; char str1[5]; strcpy(str1, str); //char* str1 = (char*)"Alex"; fprintf(stdout, "the address of str1 is %p (%u)\n", str1, str1); fprintf(stdout, "the value of str1 is (%u) [%c]\n", str1, *str1); fprintf(stdout, "the value of str1 is (%u) [%c]\n", str1 + 1, *(str1+1)); fprintf(stdout, "the value of str1 is (%u) [%c]\n", str1 + 2, *(str1+2)); fprintf(stdout, "the value of str1 is (%u) [%c]\n", str1 + 3, *(str1+3)); fprintf(stdout, "the value of str1 is (%u) [%d]\n", str1 + 4, *(str1+4)); *(str1 + 2) = 0; fprintf(stdout, "modified str1 is [%s]\n", str1); //char str2[] = "Alex"; //fprintf(stdout, "str2 = %s [%u] [%p]\n", str2, str2, 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 (%p)\n", argv[1], argv[1]); /* // char** x = argv; //fprintf(stdout, "x = %p (%u), argv = %p (%u)\n", x, x, argv[1], argv[1]); // float pointer // float x = (float)999.0; fprintf(stdout, "the value of x = %f\n", x); fprintf(stdout, "the address of x = %p (%u)\n", &x, &x); fprintf(stdout, "the value of a pointer = %f\n", *(&x)); */ float* v = new float[3]; v[0] = 1.0; v[1] = -1.0; v[2] = 99.0; memset(v, 0, 3 * sizeof(float)); fprintf(stdout, "v[0] (address %u) = %f\n", &v[0], v[0]); fprintf(stdout, "v[1] (address %u) = %f\n", &v[1], v[1]); float** w = new float*[3]; w[0] = new float[99]; w[1] = new float[33]; w[2] = new float[19]; w[99][12828283] = 27.27; w[1][15] = 27.27; w[2][18] = 27.27; fprintf(stdout, "address of w[0] = %u, value = %u\n", &w[0], w[0]); fprintf(stdout, "address of w[1] = %u, value = %u\n", &w[1], w[1]); fprintf(stdout, "address of w[2] = %u, value = %u\n", &w[2], w[2]); /* fprintf(stdout, "argv is located at %p (%u)\n", argv, argv); fprintf(stdout, "argv[0] is located at %p (%u)\n", &argv[0], &argv[0]); fprintf(stdout, "argv[0] value is %s\n", argv[0]); fprintf(stdout, "argv[0][0] value is %c\n", argv[0][0]); fprintf(stdout, "argv[0][1] value is %c\n", argv[0][1]); fprintf(stdout, "argv[1] is located at %p (%u)\n", &argv[1], &argv[1]); /* fprintf(stdout, "argv[1] value is %s\n", argv[1]); fprintf(stdout, "argv[1][0] value is %c\n", argv[1][0]); fprintf(stdout, "argv[1][1] value is %c\n", argv[1][1]); fprintf(stdout, "argv[1][3] value is %u\n", argv[1][3]); fprintf(stdout, "argv[1][4] value is %c %u\n", argv[1][4], argv[1][4]); */ // exit gracefully // exit(0); } bool myfunct(float* x) { float* joe = new float[10]; *x = 99.0; // changes the value of x in the calling program because it is a pointer return true; }