-4

I have written a program with a function that looks at a two dimensional array ir_data[60][768].

The While loop should call the "Hotspotberechnung" function only when there is a value that equals or is over 30 in a given row. "Hotspotberechnung" should store the given row in an array[24][32] and then print the array.

The problem is that the program prints an array without any values equal or over 30.

Unfortunately, I cannot find the mistake on my own.

The While Loop:

int8_t Temperatur[768] = {0};

while (get_ir_data( &Temperatur[0], sizeof(Temperatur)/sizeof(Temperatur[0])) == 0)
{
   for(int k=0; k<768; k++)
   {
       if(Temperatur[k] >= 30)
       {
          Hotspotberechnung(Temperatur,bild);
       }

    }
}

The function :

bool Hotspotberechnung(int8_t tabelle1[768],int8_t tabelle2[24][32])
    {
        int i=0, j=0, x=0, y=0;

        for(j=0; j<24; j++)                             //Tabellen werden gefüllt
        {
            for(i=0; i<32; i++)
            {
                tabelle2[j][i] = tabelle1[(j*24)+i];
            }
        }
        for(j=0; j<24; j++)
        {
            for(i=0; i<32; i++)
            {
                printf("%d.%d=%d\n",j,i,tabelle2[j][i]);
            }
        }

get_ir_data :

int8_t get_ir_data(int8_t* data, int n)
{
    static uint8_t idx = 0;

    if (n != 24 * 32) {
        printf("Invalid size of array data!\n");
        return -1;
    }

    memcpy(data, ir_data[idx], n);
    idx++;

    if (idx >= sizeof(ir_data) / sizeof(*ir_data))
        return -2;

    return 0;
}
  • 4
    Edit the question to provide a [mre]. – Eric Postpischil Jun 03 '22 at 14:08
  • Put `printf("k = %d, Temp = %d\n", k, Temperatur[k]);` in the `if` block to see what the values are. – Barmar Jun 03 '22 at 14:09
  • 3
    `sizeof(ir_data) / sizeof(*ir_data)` is likely not doing what you think it does. – Rogue Jun 03 '22 at 14:10
  • 1
    What is `Temperatur` in your first snippet? Is it an array, a pointer, an array argument to your function (which actually is again a pointer)? – Gerhardh Jun 03 '22 at 14:10
  • @Rogue That might be a mistake, but it's hard to see how it could cause this symptom. Especially since `get_ir_data()` checks that `n` is the expected value. – Barmar Jun 03 '22 at 14:11
  • Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. This allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel Jun 03 '22 at 14:13
  • @Barmar but `n` is set by `sizeof(Temperatur) / sizeof(Temperatur[0])`, potentially a similar issue. – Rogue Jun 03 '22 at 14:14
  • @Rogue I know, but the function then does `if (n != 24 * 32)` to check that it's the correct value. – Barmar Jun 03 '22 at 14:16
  • Oh, we're talking about different variables that do this. Sorry. – Barmar Jun 03 '22 at 14:17
  • @Rogue: There is no indication that `ir_data` is a pointer, not an array. The first sentence of the posted describes it as an array. Please do not mark questions as duplicates based on speculation. – Eric Postpischil Jun 03 '22 at 14:18
  • @Gerhardh Temperatur is an array. I assume that get_ir_data puts different rows of ir_data in Tempretaur. – Luca Schneider Jun 03 '22 at 14:18
  • @LucaSchneider can you edit your question to show the declarations for `Temperatur` and `ir_data`? – Rogue Jun 03 '22 at 14:22
  • @Barmar i put the printf in the if block. It then prints k = 636, Temp = 30. But when I print my array at the 636 spot there is no temp equal to 30 – Luca Schneider Jun 03 '22 at 14:22
  • 1
    Please show code, rather than describe it. "two dimensional array ir_data[60][768]"? Why say that? I want to see the actual declaration. By the way, having all your identifiers and comments in English could be a good idea. What if you need to ask some overseas colleagues for help and they cannot understand what the program is doing? – n. m. could be an AI Jun 03 '22 at 14:25
  • @Rogue ir_date is an extra headerdata it is delclared there as int8_t ir_data[60][24 * 32] = ... . There are to many values there so i cannnot copy and paste the hole thing here – Luca Schneider Jun 03 '22 at 14:32
  • Note that an array passed to a function is passed as a pointer. Values in the brackets don't do anything; they are just comments. _Eg_, `int a[42]` is the same as `int *const a` as a parameter. – Neil Jun 03 '22 at 14:45
  • 1
    @Neil: `int a[42]` is not the same as `int *const a`; there is no `const` for the array. – Eric Postpischil Jun 03 '22 at 14:53
  • 1
    @Neil While the size in brackets has no technical effect, it's useful for stating intention. It saves you having to write that as an actual comment `// a points to an array of 42 elements` – Barmar Jun 03 '22 at 14:55
  • [C-FAQ Question 6.4](http://c-faq.com/aryptr/aryptrparam.html) has more details. @EricPostpischil correct, my bad. – Neil Jun 03 '22 at 15:14

1 Answers1

1
tabelle1[(j*24)+i]

This index calculation is wrong. You need

tabelle1[(j*32)+i]

To verify, calculate the maximal value of (j*24)+i, given the ranges of j (0 to 23) and j (0 to 31). Is it 767?

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243