1

I have the following 2d array of unsigned chars to store an image as values from 0 to 255:

unsigned char img[MAX_DIM][MAX_DIM];

And I have a function that takes a 2d int array as parameter:

void transpose(int m[][MAX_DIM], int h, int w);

How can I pass the char array to that function? I tried something like this but it doesn't work:

transpose((int (*)[MAX_DIM])img, h, w);
10
  • You can't pass an unsigned char [][] array to a function accepting an int[][]array. Decide which type you wish to use then use that type consistently. Commented Jan 14, 2021 at 13:45
  • @Lundin why can't I just cast it? Commented Jan 14, 2021 at 13:46
  • Because the types are nowhere near compatible, they have different sizes. Commented Jan 14, 2021 at 13:46
  • 1
    Because a cast (int)my_char applies to that one single variable, which in this case gets converted. If you were to do the same for a 2D array, you would have to loop through the 2D char array and cast each individual item to a 2D int. That works but is very slow. Why must you use unsigned char in the first place? Commented Jan 14, 2021 at 13:52
  • 1
    Therefore, should I change transpose to use unsigned char? Commented Jan 14, 2021 at 13:55

2 Answers 2

1

You could make a type-generic interface and implement two different transpose functions, one for 8 bit data and one for 32 bit data. Example:

#include <stdint.h>
#include <stdio.h>

void transpose_32 (size_t h, size_t w, uint32_t m[h][w]){ puts(__func__); }
void transpose_8  (size_t h, size_t w, uint8_t  m[h][w]){ puts(__func__); }

#define transpose(m,h,w) _Generic(**(m), uint8_t: transpose_8, uint32_t: transpose_32)(h,w,m)

int main(void)
{
  const size_t MAX_DIM = 10;
  uint8_t  img1[MAX_DIM][MAX_DIM];
  uint32_t img2[MAX_DIM][MAX_DIM];
  
  transpose(img1, MAX_DIM, MAX_DIM);
  transpose(img2, MAX_DIM, MAX_DIM);

  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would like to point out a few things that might help you.

First, if you want to pass an array as a function parameter, don't specify the size in brackets because there is no need to. In fact, think it is wrong.

Second, when you want to typecast something to int as I guess you are trying at your third line of code, the expression is : (int)charArray[] ; and not int charArray(*) [SIZE]

Third and most important, you cant typecast something that is non-arithmetic (char) to something that is (int). But what you can do is use the atoi function that converts an argument to int.

int atoi(const char *str);

Also, next time you want to pass an array to a function, you should prefer to reference it and pass the address of the array as a parameter and not the array with the content itself, because there is no way to return the whole array converted from the transpose function, even if you manage to process it the way you want.

I hope I helped even a little, but if your problem is not resolved, try posting the statement of your problem in order for us to be more able to help you.

5 Comments

char can be used for storing arithmetic data just fine, it's just an 8 bit integer. Don't use atoi regardless, it should be avoided because of the crappy error handling, use strtoul instead.
" you should prefer to reference it and pass the address of the array as a parameter and not the array with the content itself" The code does this. You can't pass arrays by value in C.
Despite I disagree with you, for a quite significant amount of reasons, I would prefer not to give an answer to a confused person without reviewing the full statement first, because the question is a bit unclear on itself.
"You can't pass arrays by value in C " . Well, yes, but Stepii isn't passing them by reference correctly either .
void transpose(int m[][MAX_DIM], int h, int w); is perfectly fine and passing "by reference".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.