// file: my first program // #include #include #include // function: myprint // // arguments: // FILE* fp: open file pointer // float* x: a floating point array // long N: the total number of elements // // return: a logical value indicating status // bool myprint(FILE* fp_a, float* x_a, long N_a) { // print // for (long i = 0; i < N_a; i++) { fprintf(stdout, "myprint: (%d) = %f\n", i, x_a[i]); } // exit gracefully // return true; } // function: mychange // // arguments: // float* x: a floating point array // long N: the total number of elements // // return: a logical value indicating status // bool mychange(float* x_a, long N_a) { // loop over the matrix // for (long i = 0; i < N_a; i++) { *x_a++ = (float)27.0; } // exit gracefully // return true; } // function: mycopy // // arguments: // float* x: a floating point array // long N: the total number of elements // // return: a float* containing the new array // float* mycopy(float* x_a, long N_a) { // allocate memory // // float* rval = malloc(N_a * sizeof(float)); float* rval = new float[N_a]; // create a temp pointer // float* tmp_val = rval; // copy the data: // we could use memcpy() // // memcpy(rval, x_a, N_a * sizeof(float)); for (long i = 0; i < N_a; i++) { *tmp_val++ = *x_a++; } // exit gracefully // return rval; } // my main program starts here // int main(int argc, char** argv) { // grab the dimensions from the command line // long Mrows = atoi(argv[1]); long Ncols = atoi(argv[2]); long Pdepth = atoi(argv[3]); // declare an array of floats // float x[Mrows][Ncols][Pdepth]; // set the values // for (long m = 0; m < Mrows; m++) { for (long n = 0; n < Ncols; n++) { for (long p = 0; p < Pdepth; p++) { x[m][n][p] = (float)(m + n + p); fprintf(stdout, "[%d][%d][%d] = %f\n", m, n, p, x[m][n][p]); } } } float* ptr = (float*)x; fprintf(stdout, "the memory location is %u\n", x); fprintf(stdout, "data: \n"); for (long i = 0; i < Mrows * Ncols * Pdepth; i++) { fprintf(stdout, "(%d: %u): %f\n", i, ptr, *ptr); ptr++; } // print x // myprint(stdout, (float*)x, Mrows * Ncols * Pdepth); // change x // mychange((float*)x, Mrows * Ncols * Pdepth); fprintf(stdout, "New values:\n"); myprint(stdout, (float*)x, Mrows * Ncols * Pdepth); // create a new array // float* y = mycopy((float*)x, Mrows * Ncols * Pdepth); fprintf(stdout, "Copied values:\n"); myprint(stdout, (float*)y, Mrows * Ncols * Pdepth); delete [] y; /* long M = 10; float* y = new float[M*M]; memset(y, 0, M*M*sizeof(float)); y[M*1 + 1] = 27.0; y[M*2 + 2] = 99.0; float*z = y; long N = 3; for (long i = 0; i < N; i++) { for (long j = 0; j < N; j++) { fprintf(stdout, "y[%d][%d] = (%u) %f <> %u %f\n", i, j, &y[N*i+j], y[N*i+j], z, *z); z++; } } /* short int buf[10]; FILE* fp = fopen(argv[1], "r"); fprintf(stdout, "position = %u\n", ftell(fp)); fread(buf, sizeof(short int), 1, fp); fprintf(stdout, "position = %u\n", ftell(fp)); fclose(fp); /* void* pstr = (void*)new char[10]; char Joe[] = "JoePicone"; char* str = Joe; fprintf(stdout, "str = %c\n", str[0]); str++; fprintf(stdout, "str = %c\n", *str); double Mary[10]; Mary[0] = 27.0; Mary[1] = 35.0; Mary[2] = -27.0; double* psum = (double*)Mary; fprintf(stdout, "psum = %f\n", *psum); psum++; fprintf(stdout, "psum = %f\n", *psum); */ // exit gracefully // return 0; }