C/C++ Why Aren't My C++ Array Functions Working?

AI Thread Summary
The discussion centers around troubleshooting C++ functions for manipulating a 2D array. Key issues include the incorrect use of pointers in the function declaration for `flipsite`, which should accept a 2D array directly rather than as a pointer to pointers. The correct declaration is `void flipsite(double pgrid[][10], int n, int m)`, allowing direct modification of the array elements. Additionally, participants emphasize the importance of zero-based indexing in C++, cautioning against out-of-bounds access, which can lead to undefined behavior. They clarify that arrays are passed by reference, allowing functions to modify the original array without needing to return it. The conversation also touches on the misconception that arrays can be returned from functions, which is not allowed in C++. Instead, pointers or references should be used for such operations. Overall, the thread provides insights into proper array handling and function definitions in C++.
villiami
Messages
27
Reaction score
0
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? //---------Functions

//-- 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]

flipsite(grid,1,1);
grid2 = newgrid(grid);

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

//Thanks!
 
Technology news on Phys.org
villiami said:
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?


//---------Functions

//-- 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]

flipsite(grid,1,1);
grid2 = newgrid(grid);

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

//Thanks!

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
 
p1ayaone1 said:
int arr[3] = {16}; // creates an array with three elements and makes them all equal 16
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.
p1ayaone1 said:
int arr2[3]; // creates an empty array
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.
p1ayaone1 said:
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!
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.
 
I stand corrected, thanks.
 
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!
 
villiami said:
void flipsite(double* pgrid[][10], int n, int m)
{*pgrid[n][m] = *pgrid[n][m] + 1.0;
}
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 "*".
 
If you would beloeve it, Dev C++ ouputs "1" for the following code:
Code:
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
    cout<<pgrid[0][120]<<endl;
    system("pause");
    return 0;
}
 
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.
 
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!
 
  • #10
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.
 
  • #11
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.
 

Similar threads

Replies
25
Views
2K
Replies
5
Views
3K
Replies
1
Views
1K
Replies
23
Views
2K
Replies
39
Views
4K
Back
Top