Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How cast array[4] to array[2][2]?

Tags:

arrays

c

casting

I have a array short frame[4] and I want it as a function parameter as short frame[2][2]

How can I cast it? I tried different things (like *(short [2][2])&frame[0]*), but I still get error messages.

Also not working is if I declare the function with void function(short frame[2][2]) and call the function with function(&frame[0]) while frame is a short frame[4];

like image 563
user1859796 Avatar asked Apr 22 '26 21:04

user1859796


2 Answers

I don't think it is a good pratice, anyway:

f((short (*)[2])a);
like image 175
md5 Avatar answered Apr 24 '26 11:04

md5


This works here, albeit with a warning.

#include <stdio.h>
void function(short frame[2][2])
{
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      printf("%d ", frame[i][j]);
  printf("\n");
}
int main()
{
  short frame[4] = { 0, 1, 2, 3 };
  function(&frame[0]);
  return 0;
}

What error message do you get?

like image 45
kmkaplan Avatar answered Apr 24 '26 12:04

kmkaplan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!