0

Basically, I'm saving some coordinates in 2 arrays, arrayX and arrayY. Then, I want to change the value of a "blank" matrix in the coordinates that are saved in the previously mentioned arrays. Will post a simpler version of the code, just so you can understand:

float* arrayX = 0;
float* arrayY = 0;
int array_size = 0;
int width = 1440;
int height = 1080;

for (i=0;i<1234;i++){
    if(flag==TRUE){
        arrayX = realloc(arrayX, ++array_size * sizeof(*arrayX));
        arrayY = realloc(arrayY, array_size * sizeof(*arrayY));
        arrayX[array_size-1] = i
        arrayY[array_size-1] = 100;
    }
}

float *matrix1 = (float *)malloc(width * height * sizeof(float*));
for (i=0; i<width*height;i++){
    *(matrix+i) = 0;
}
for (i=0; i<array_size; i++){
    *(matrix1 + arrayY[i] * width + arrayX[i]) = 255;  //ERROR HERE
}

This code gives me this error:

error: invalid operands to binary + (have 'float *' and 'float')

And I'm not understanding why:

arrayY[l] is the float pointed by arrayY + l.

*(matrix1 + float * int + float) should also be a float.

I'm clearly messing up something here, and I've scrolled to a fair share of other posts (on how to allocate the matrix, and with this error) and don't understand it/don't find exactly what I'm looking for.

I'm sure it's a pretty silly question, and with a very easy solution.

  • Do your `arrayY` and `arrayX` need to be arrays of floats in your actual code? If not then they could just be `int` arrays. Otherwise, try casting the values to floats, i.e., `*(matrix1 + (int)arrayY[l] * width + (int)arrayX[l]) = 255;` – Matt Pitkin May 02 '23 at 09:13
  • Also, maybe have a look at https://stackoverflow.com/questions/394767/pointer-arithmetic about pointer arithmetic. In pointer arithmetic, the things you add to pointers should be integers. – Matt Pitkin May 02 '23 at 09:18
  • 1
    You're trying to add a `float` to a pointer. That's invalid. What type would the result even have? You need to add an integer type to a pointer. Or you need to add a float and another numeric type. – Tom Karzes May 02 '23 at 09:34
  • `array_size` is uninitialized. Also, what is `array1_size`? – Ian Abbott May 02 '23 at 10:52
  • @IanAbbott those are both transcription errors. Nothing to be worried about, but thanks for pointing them, already edited. – ATSlooking4things May 02 '23 at 11:23
  • 1
    I guess the index `l` is also a transcription error (should be `i`)? – Ian Abbott May 02 '23 at 13:33
  • @ATSlooking4things Aside: notice the difference between `width * height * sizeof(float*)` and `sizeof(float*) * width * height`. Which is better when `width` or `height` is large? – chux - Reinstate Monica May 02 '23 at 13:48
  • @IanAbbott yes. It's not easy to simplify a bit of code to Stack Overflow. My code has very extensive variable names and lots of other code-bits that were irrelevant to this problem. I always try my best to leave it as understandable as possible for you guys, but this time I've made some mistakes. So, sorry for making you lose time with this specifics – ATSlooking4things May 02 '23 at 14:01

1 Answers1

1

For starters the argument of the call of malloc in this declaration

float *matrix1 = (float *)malloc(width * height * sizeof(float*));
                                                  ^^^^^^^^^^^^^^

is incorrect. It seems you mean

float *matrix1 = (float *)malloc(width * height * sizeof(float));
                                                  ^^^^^^^^^^^^^

The pointer arithmetic is allowed between pointers and objects of integer types. However in this expression

*(matrix1 + arrayY[l] * width + arrayX[l]) 

you are using pointers and objects of the type float. So the compiler issues the error message.

From the C11 Standard (6.5.6 Additive operators)

2 For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type. (Incrementing is equivalent to adding 1.)

So either use integer arrays or at least use casting as for example

*(matrix1 + ( int )( arrayY[l] * width + arrayX[l] ))
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks. I doesn't give me building errors anymore. But, it's not doing what I want either. If I fill the new matrix with 255 (white) instead of 0 (black), and then save it using `stbi_write_bmp("answer_blank.bmp", width, height, 1, matrix);` I get an image with alternating black and white columns. It seems that my `for (i=0; i – ATSlooking4things May 02 '23 at 10:06
  • 2
    @ATSlooking4things You can ask a new question (closing this question y electing the best answer) providing a minimal complete updated program. – Vlad from Moscow May 02 '23 at 10:20