I'm having a lot of difficulty assigning and referencing values from a 2D array. The exact error I'm getting, on the line with the ">", is: invalid types 'int [5][int [7]]' for array subscript.
What I would like to do is simply assign the value in "spectrumPeaks[ i ]" to the indicated indices in "spectrumPeakArray[ i ][peakCount]". I don't understand the error because everything is an int, as far as I can tell from the various array tutorials, you simply access the array by [ i ][ j ]. Can anyone help me make sense of this?
#define peakSamples 5 //number of peaks to average over
#define freqChannels 7
int spectrumPeaks[7] = {0}; // holds peak values
int spectrumPeakSum[7];
int spectrumPeakAvg[7];
bool peakFlag[7] = {0};
int lastPeak[7] = {0};
int peakCount[7] = {0};
int spectrumPeakArray[freqChannels][peakSamples];
for (int i = 0; i < 7; i++) {
if ( spectrumPeaks[i] < lastPeak[i] && peakFlag[i] == 0 ) {
spectrumPeakSum[i] += spectrumPeaks[i]- spectrumPeakArray[i][peakCount];
spectrumPeakAvg[i] = spectrumPeakSum[i] / peakSamples;
> spectrumPeakArray[i][peakCount] = spectrumPeaks[i];
peakFlag[i] = 1;
peakCount[i] = (peakCount[i] + 1) % peakSamples;
}
}
I can't try to compile your program because it's incomplete: No setup() function, no loop(), and your 'for loop' is not inside any function.
You can’t use an array to index an array. If you want to use each element of an array to index into an array you have to put a loop around it all to step through the elements.
johnwasser:
You can’t use an array to index an array. If you want to use each element of an array to index into an array you have to put a loop around it all to step through the elements.
I see. I think there's a bigger problem here though. I can eliminate the second array by assigning spectrumPeaks[ i ] to a temporary variable, e.g.
int spectrumPeaksTemp = spectrumPeaks[ i ];
spectrumPeakArray[ i ][peakCount] = spectrumPeakTemp;
But I still receive the same error, even without the other array. It seems I just can't assign a value to a specific position in the 2D array spectrumPeakArray.
gfvalvo:
I can't try to compile your program because it's incomplete: No setup() function, no loop(), and your 'for loop' is not inside any function.
The code is long and messy and requires a number of other libraries, so it won't run either way. I can post it all if needed, but this is the only relevant part and I believe has all the required definitions. Variables are defined outside the main loop, and the for loop in in the main loop.
dlovinger:
I see. I think there's a bigger problem here though. I can eliminate the second array by assigning spectrumPeaks[ i ] to a temporary variable, e.g.
int spectrumPeaksTemp = spectrumPeaks[ i ];
spectrumPeakArray[ i ][peakCount] = spectrumPeakTemp;
But I still receive the same error, even without the other array. It seems I just can't assign a value to a specific position in the 2D array spectrumPeakArray.
You can't use an array as an index value.
See that [peakCount] you're doing? peakCount is not an integer, it's an array of integers. You can't use it there. You can use one of the array values there (like arrayCount[3]), but you can't use the entire array as an index value.
Jiggy-Ninja:
You can't use an array as an index value.
See that [peakCount] you're doing? peakCount is not an integer, it's an array of integers. You can't use it there. You can use one of the array values there (like arrayCount[3]), but you can't use the entire array as an index value.
Oh wow.. of course. I've been staring at this for a while, thinking I need to learn about pointers, etc, in order to do something simple. I can't believe I missed that. Nothing to see here, carry on. Thank you.
dlovinger:
The code is long and messy and requires a number of other libraries, so it won't run either way.
You're right, nobody wants to wade through a bunch of unrelated, ugly code. When that's the case, you should post an MCVE. That's the smallest complete code possible that either:
- Compiles and demonstrates the same run-time problem as your larger, ugly code.
or
- Produces the same compiler errors that your larger, ugly code does.
Only include the essentials necessary for the above, leave out all the clutter.
One benefit of creating an MCVE is that
dlovinger:
Oh wow.. of course. I've been staring at this for a while, thinking I need to learn about pointers, etc, in order to do something simple. I can't believe I missed that. Nothing to see here, carry on. Thank you.
Try picking better names. At a bare minimum, using plural form for arrays can distinguish between something that represents a single value vs. multiple values.