How can I pass a 2D array in C++ without errors?

In summary, the conversation revolves around the correct way to pass a two-dimensional array from the main function to a secondary function. The correct declaration for the pointer to the array is discussed, as well as the error that is being received while compiling. Various attempts to solve the issue, including using the & operator and * to force the passing of a pointer, are mentioned. It is also suggested to use pointer arithmetic instead of index lookups for efficiency. However, it is clarified that the primary reason for using ragged arrays over multi-dimensional arrays in C/C++ is due to the language's weakness in handling multi-dimensional arrays efficiently.
  • #1
giritrobbins
8
0
1. The problem I am having is that I am trying to pass a two dimensional array from my main function to a secondary function. I know it has to be call by reference and using the name of the matrix, "matrix" in my case should send the location. Also since it has more than one dimension there needs to be an inclusion for the number of columns so the function declaration becomes:

float average_depth(float matrix[][6], int size);

where matrix is the matrix memory location, and int size is the total number of elements. From the two lines in my book about this and what I could find online this is the correct declaration for the pointer to the array. But I keep getting an error in the compiling. I have tried restarting Microsoft Visual Studio and reopening my code but it still does not work.

2. I have tried a number of things. Since the error code I keep on getting is an error code that says:

subscript requires array or pointer type

I think this means that I am not correctly passing the pointer, or possibly not using it correctly.
declaration:
float average_depth(float matrix[][6], int size);
call:
actualaverage=average_depth(matrix, 36);
function:
float average_depth(float matrix, int size)
{
float average;
for(int i=0;i<=size;i++)
{
average=average+matrix;
}
return average;
}

It is a six by six matrix so do I need to have size be 6 or 36. This is of course if I can figure out what is going wrong.

I have tried including the & operator and the * to force the passing of a pointer but that seemed to generate more errors. There is also a copy of the program if you want to look at it. I am sure it is something relatively simple but I have never used a pointer before and am sure that the finer points have been lost on me.
 

Attachments

  • program.txt
    1.9 KB · Views: 563
Technology news on Phys.org
  • #2
look at your function prototype and your function definition ...do they match...

just to tell you how to determine the error : the error is targeting your function implementation...and says the following regarding matrix (hopefully when you compiled with MSVC it would have told you the line)
"subscript requires array or pointer type"... what do you think is subscript referring to in your line of code and what is the array or ptr type its looking for. When you've answered the first question above this shouldbe straight forward.

if at all having problems using [][] passing u can use ** which is the same. Just requires the user to know about what needs to be in the parameter list.
 
Last edited:
  • #3
By the way use pointer arithmetic instead of index lookups in calculating the average, that is much more efficient. Likely in this simple case the compiler already optimizes this for you but it is a good program practice to get used to it when it matters.

C and C++ allows you to use pointers so why not use them?
 
  • #4
giritrobbins said:
I keep on getting is an error code that says:

subscript requires array or pointer type

I think this means that I am not correctly passing the pointer, or possibly not using it correctly.

What this means is that you are declaring matrix to be a scalar in your function definition. You cannot index a scalar. Here is your function definition:
function:
float average_depth(float matrix, int size)

This doesn't match the prototype
declaration:
float average_depth(float matrix[][6], int size);

The size passed in the call doesn't match either the matrix size:
call:
actualaverage=average_depth(matrix, 36);

It should be 6, not 36.
 
  • #5
MeJennifer said:
By the way use pointer arithmetic instead of index lookups in calculating the average, that is much more efficient.

First, he is passing a pointer.

Second, it is a common misperception in C/C++ that ragged arrays are "more efficient" that multi-dimensional arrays. This is untrue. A ragged array,

double ** matrix

requires two index calculations and two pointer references to access an element of the array. A two dimensional array,

double matrix[6][6]

requires one index calculation and one pointer reference to access an element of the array. The index calculation for the multi-dimension array involves integer multiplication while the index calculations for the ragged arrays can be optimized into shifts. However, the pointer references involve memory access. Even if the memory is cached, accessing memory is much much slower than index calculations, which are done with registers. The cost of a double memory access vastly overwhelms the small gain in register-based index calculations.

The primary reason for using ragged arrays over multi-dimensional arrays in C/C++ is that multi-dimensional arrays are one of the weak spots in the language.
 
Last edited:
  • #6
D H said:
First, he is passing a pointer.

Second, it is a common misperception in C/C++ that ragged arrays are "more efficient" that multi-dimensional arrays. This is untrue. A ragged array,

double ** matrix

requires two index calculations and two pointer references to access an element of the array. A two dimensional array,

double matrix[6][6]

requires one index calculation and one pointer reference to access an element of the array. The index calculation for the multi-dimension array involves integer multiplication while the index calculations for the ragged arrays can be optimized into shifts. However, the pointer references involve memory access. Even if the memory is cached, accessing memory is much much slower than index calculations, which are done with registers. The cost of a double memory access vastly overwhelms the small gain in register-based index calculations.

The primary reason for using ragged arrays over multi-dimensional arrays in C/C++ is that multi-dimensional arrays are one of the weak spots in the language.
I think you misunderstand what I am saying.

If someone wants to access or update an array in a sequential fashion there is no point in doing index lookups, just increment a pointer. Much more efficient.
 
  • #7
MeJennifer said:
If someone wants to access or update an array in a sequential fashion there is no point in doing index lookups, just increment a pointer. Much more efficient.

Can you provide an example where this true? In all the examples I've tried, I could not get a difference in the assembly between pointer arithmetic and array indexing. Given that array indexing may be less error prone, I'd prefer that. Or are you talking about STL type range algorithms?
 
  • #8
int a[#]; for(i=0;i<n;i++) *a++=1; i don't think for(;a++;) will work because it iwll just keep accessing contiguous memory addys. and won't terminate though you can try. for(i=n;i;)*a++=1;
 
  • #9
neurocomp2003 said:
int a[#]; for(i=0;i<n;i++) *a++=1; i don't think for(;a++;) will work because it iwll just keep accessing contiguous memory addys. and won't terminate though you can try. for(i=n;i;)*a++=1;

I gave "for(i=0; i < n; ++i) *a++ = 1;" a try; gcc 3.2 x86 -O3 gave the same # of instructions as array indexing "for( i = 0; i < n; ++i) a = 1;".

Compilers seem pretty good at seeing this kind of thing. For sure, 2D arrays can be done wrong: I avoid vector<vector> type code for matrices, since the additional indirection does hurt. But how would pointer arithmetic will bail one out of that?
 

1. What is a 2D array in C++?

A 2D array in C++ is a data structure that stores elements in a two-dimensional grid or table. It is also known as a matrix and is used to represent data that has both rows and columns, such as a spreadsheet.

2. How do you pass a 2D array in C++?

To pass a 2D array in C++, you can use a pointer to a pointer or a reference to an array. Both methods allow you to access and modify the elements of the array within a function.

3. Why is passing a 2D array in C++ different from passing a 1D array?

In C++, a 2D array is passed as a pointer to an array of arrays, while a 1D array is passed as a pointer to a single array. This is because a 2D array is a collection of 1D arrays, and its elements are stored in a contiguous block of memory.

4. Can you pass a 2D array by value in C++?

No, you cannot pass a 2D array by value in C++. When you pass an array by value, a copy of the array is created, and any modifications made to the copy will not reflect in the original array. To modify a 2D array, you must pass it by reference or by pointer.

5. How do you access elements of a 2D array in C++?

You can access elements of a 2D array in C++ using a combination of row and column indices. For example, to access the element at row 2 and column 3, you would use array[2][3]. You can also use nested for loops to iterate through the rows and columns of the array.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
713
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
845
  • Programming and Computer Science
Replies
2
Views
1K
Replies
3
Views
718
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top