Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help pls! C++ function of array

  1. Apr 9, 2010 #1
    Have read a million different sites and threads, but still not getting anywhere (damn 1st year java makes addresses incomprehensible!).

    What changes do I need to make to get these c++ functions to work?


    //-- A function to increment the (n,m) element
    void flipsite(double* pgrid[][10], int n, int m)
    {*pgrid[n][m] = *pgrid[n][m] + 1;

    //-- function to output the array. A form using pointers is fine if that's easier.

    double[][10] newgrid(double grid[][10])
    {return grid;

    void main() {
    // I first define an array like:
    double grid[5][10];
    double grid2[5][10]

    grid2 = newgrid(grid);

    std::cout << grid[1][1] << ", " << grid2[1][1] << std::endl; //should output "1, 1"

  2. jcsd
  3. Apr 9, 2010 #2
    lol java. in c/c++ arrays can be a bit tricky as you've discovered. no offense or anything, but that code is wrong in a lot of ways.

    here are some hings to remember:

    1) array indexing is zero-based:
    double arr[5]; // creates an array with 5 elements
    arr[0] = 1; // assigns the value 1 to the zeroth element
    cout<<arr[5]; // !!!!!!!!!! there is no 5th element - they are numbered 0-4. the compiler will NOT care about this, you might get an error when you run your program. you might not, and have a "garbage" value 8.03474e-645 so something strange like that.

    2) you cannot return an array from a function
    double[3] foo(int num1, int num2){...}
    is simply not allowed.

    3) the name of an array is a pointer
    int arr[3] = {16}; // creates an array with three elements and makes them all equal 16
    int arr2[3]; // creates an empty array
    arr2 = arr1; // not what you think. arr1 and arr2 are both of type int*, so your are writing the address of arr1 to the int* arr2. this is very bad becuse you;ve allocated memory for arr2, but since you overwrote the address, you have no way of accessing that memory anymore!

    4) arrays are passed-by-reference to functions by default. that's how you get around the fact you cannot return an array from a function.

    I really don't know what your code is trying to do, maybe clarify its purpose and we could help you out some more
  4. Apr 9, 2010 #3


    Staff: Mentor

    Correction: arr[0] is initialized to 16, but the other two elements are initialized with whatever happens to be on the stack or 0, depending on whether arr is a local variable or global/statiic variable.
    Creates an array, but it won't be empty. There will be three numbers in it. Incidentally, an int array will never have a garbage value of 8.03474e-645. That's a floating point value. int variables have int-type garbage values.
    Neither arr1 nor arr2 is an "lvalue" so the compiler flags this assignment as an error. The name of an array is read-only; you can't change its value.
  5. Apr 9, 2010 #4
    I stand corrected, thanks.
  6. Apr 11, 2010 #5
    Thanks for your help, I'm still unsure how to declare a function to modify a 2D array.

    I'm trying to make a function to increment a specific element:

    void flipsite(double* pgrid[][], int n, int m)
    {*pgrid[n][m] = *pgrid[n][m] + 1.0;

    So then in the main function I can write:

    double grid2[2][3] = {{0.0,0.0,0.0},{0.0,0.0,0.0}}; // An all zeros matrix
    flipsite(grid,0,2); //grid[0][2] should now be 1.0, so "std::cout << grid[0][2] << std::endl;" outputs 1.

    Should the function declaration (and how I refer to the array for the increment command) be different? Do I need to specify the pgrid array's size?

    Thanks again!
  7. Apr 11, 2010 #6


    User Avatar
    Homework Helper

    That should be:
    void flipsite(double pgrid[][10], int n, int m)
    {pgrid[n][m] = pgrid[n][m] + 1.0;

    Your function prototype is for a 2D array of pointers to doubles, but your variable is a 2D array of doubles, remove the "*".
  8. Apr 11, 2010 #7
    If you would beloeve it, Dev C++ ouputs "1" for the following code:
    Code (Text):

    void flipsite(double pgrid[][10], int n, int m)
         pgrid[n][m] += 1;    
    int main()
        double pgrid[10][10] = {0};
        flipsite(pgrid,0,120);  // giving an out-of-bouds index, expecting run-time error
        return 0;
  9. Apr 11, 2010 #8


    User Avatar

    Staff: Mentor

    Out-of-bounds array indices don't automatically generate run-time errors in C or in C++. That's why they're so dangerous.

    You get a run-time error only if the index goes so far out of bounds that the program tries to access protected memory, or if the data at that location causes some other kind of run-time error (e.g. division by zero) which can of course also happen with bad data in the array itself.
  10. Apr 11, 2010 #9
    Great! Thanks guys. For some reason I was sure that wouldn't work- I'd read something about its value being passed, so modifying it (without referring to its address) wouldn't actually change the object.

    Thanks again!
  11. Apr 11, 2010 #10


    Staff: Mentor

    Parameters in C and C++ are passed by value rather than by reference, but if the thing being passed is a point of some kind, the function can modify what the pointer is pointing to.
  12. Apr 11, 2010 #11


    User Avatar
    Homework Helper

    Some C compilers include debug versions that check for out of bounds conditions, but this only works on arrays that include size information. There's no check on generic pointers, other than out of bound references that go beyond the virtual address space setup for a program.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook