0

Here is my .h file. I'm creating a 12x1860 2-D array, which I would like to fill inside of my .c file.

/*
 * spectrogramMatrices.h
 */

#ifndef SPECTROGRAMMATRICES_H_
#define SPECTROGRAMMATRICES_H_


//global variables

extern int binaryFingerprint[12][1860];

void makeBinFingerprint(int shortBinSpectrum[12][186], int count);

#endif /* SPECTROGRAMMATRICES_H_ */

In my .c file, I want to create the makeBinFingerprint() function, which fills a 12x1860 2d array with 10 different segments of 12x186 2d data. I'd really like to store this in that extern int binaryFingerprint[12][1860] 2d array. This is shown below

void makeBinFingerprint(int shortBinSpectrum[12][186], int count){
    int i;
    int j;

    for (i = 0; i<12; i++){
        for (j = 0; j<186; j++){
            binaryFingerprint[i][(j+(186*count))] = shortBinSpectrum[i][j];   // we're going to be storing a lot of zeros, which may be a problem!

        }
    }
}

Do you see any issues with me attempting it this way? Thanks

Maciej S.
  • 752
  • 10
  • 22
Rob
  • 85
  • 1
  • 4
  • What issue did you encounter? – Tugrul Ates Feb 23 '19 at 23:11
  • It seems like its an issue regarding the ide of the compiler of the board I'm using. I just wanted to know if this was a correct way of approaching this problem in c. – Rob Feb 23 '19 at 23:18
  • `extern` tells the compiler that the data is defined somewhere else. You still need to define the array in a `.c` file. See: https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c – Tugrul Ates Feb 23 '19 at 23:25
  • Better have some *abstract data type* for your matrix, like [here](https://stackoverflow.com/a/41410503/841108). Remember that C has one kind of arrays (an array of elements of same type). 2D arrays don't really exist in C, but it has arrays of arrays (they are similar, but not exactly the same, to hypothetical 2D arrays) – Basile Starynkevitch Feb 24 '19 at 07:11

1 Answers1

4

The array is declared not initialized in the header. Declaration is not instantiation; that is you have not actually created an array at all.

In the .c file you need an instantiation;

int binaryFingerprint[12][1860]; // << Instantiation

void makeBinFingerprint(int shortBinSpectrum[12][186], int count){
    int i;
    ...
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    Note that the declaration in the header file is only necessary if you need to access the array as a global from some other translation unit (.c file). Global data is best avoided; but that is a different issue. – Clifford Feb 23 '19 at 23:41