Title: Passing uint8_t array argument to the subroutine that excepts uint32_t (conversion from uint8_t array to uint32_t array)
Tags: c;arrays;type-conversion
Question: I'm having a C subroutine as below,
int sub_data (uint32_t size, uint32_t arrayB[])
int i;
for(i=0; i<size; i++)
printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
And I want pass arrayA as below to above mentioned subroutine,
uint8_t arrayA = {0x12, 0x34, 0x56, 0x78, 0x9a};
int main()
sub_data(5, arrayA);
return 0;
The issue is arrayA is of type uint8_t and sub_data excepts argument as uint32_t
My requirement is sub_data should take single word from arrayA and store inside arrayB as single word
So final print should be,
arrayB[0] = 12
arrayB[1] = 34
arrayB[2] = 56
arrayB[3] = 78
arrayB[4] = 9a.
I'm struggling to do the conversion. And all the solution I came around is to store four words of arrayA to single word of arrayB.
But I want single word of arrayA as single word of arrayB.
How do I achieve that?
Comment: Milan, Curious, why use `int i;` instead of `uint32_t i;` to use as an index counter up to `uint32_t size`?

Comment: Milan,  Tip: since it appears your compiler is not warning you about the mis-match of signed/unsigned compare, it implies you either 1) do not have all warnings enabled or 2) using a weak compiler.  To really save you time  and help with your struggles, enable all warnings.  It is faster feedback the SO.

Comment: All you can do is make an element-by-element copy of the `uint8_t` array, storing each element into an element in a `uint32_t` array. Perhaps you need to rethink the prototype for your function.

Comment: Yeah that also I can try. Problem is the actual function prototype is from some library in my tool. I can't change that. sub_data here is just for example. actual function accepts only uint32_t type of array variable and can't be changed

Comment: @chux - Reinstate Monica "int i; instead of uint32_t i;" This I'll correct. Thanks!


Here is another answer: int sub_data (uint32_t size, uint32_t arrayB[])
int i;
for(i=0; i<size; i++)
printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
uint8_t arrayA = {0x12, 0x34, 0x56, 0x78, 0x9a};
int main()
uint32_t temp[5] = {0};
int i;
for(i=0; i<5; i++)
temp[i]=arrayA[i];
sub_data(5, temp);
return 0;
Here I'm assigning data one by one to uint32_t temp from uint8_t arrayA like this,
uint32_t temp[5] = {0};
int i;
for(i=0; i<5; i++)
temp[i]=arrayA[i];
This works perfectly fine.
Thank you @rici for the comment.

Here is another answer: You can use generic macros and have more than one function to handle the parameter.
Example:
int sub_data_32 (uint32_t size, uint32_t *arrayB)
int i;
for(i=0; i<size; i++)
printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
int sub_data_16 (uint32_t size, uint16_t *arrayB)
int i;
for(i=0; i<size; i++)
printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
int sub_data_8 (uint32_t size, uint8_t *arrayB)
int i;
for(i=0; i<size; i++)
printf("arrayB[%0d] = %2x\n", i , arrayB[i]);
#define sub_data(a,b) _Generic((b), \
uint8_t *:  sub_data_8, \
uint16_t *: sub_data_16, \
uint32_t *: sub_data_32) (a,b)
uint8_t arrayA[] = {0x12, 0x34, 0x56, 0x78, 0x9a};
uint16_t arrayB[] = {0x13, 0x35, 0x57, 0x79, 0x9b};
uint32_t arrayC[] = {0x14, 0x36, 0x58, 0x7A, 0x9c};
int main()
sub_data(5, arrayA);
printf("-----------\n");
sub_data(5, arrayB);
printf("-----------\n");
sub_data(5, arrayC);
return 0;
https://onlinegdb.com/BJTqCPyJL
Comment for this answer: Hi, Thank you for the comment but the problem here,
```int sub_data (uint32_t size, uint32_t *arrayB)
is fixed. from the tool library.
I can't change data type to uint8_t or anything else.
And I have array of "uint8_t" which I should pass to the function "sub_data".
May be for now assigning array elements from array of uint8_t to array of uint32_t one by one looks the great option.

Here is another answer: You could use memcpy to fill a uint32_t array with each uint8_t array element, and pass it to the function:
uint8_t arrayA[5] = {0x12, 0x34, 0x56, 0x78, 0x9a};
uint32_t arrayB[5] = {0, 0, 0, 0, 0};
for (int i=0; i<5; i++)
memcpy(&amp;arrayB[i], &amp;arrayA[i], sizeof(arrayA[i]));
sub_data(5, arrayB);

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:12/24/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><arrays><type-conversion>', 'Title': 'Passing uint8_t array argument to the subroutine that excepts uint32_t (conversion from uint8_t array to uint32_t array)', 'ViewCount': 300, 'id': 59465045, 'user_AboutMe': None, 'user_AccountId': 11828634.0, 'user_DisplayName': 'Milan ', 'user_Reputation': 9.0, 'user_Views': 11.0, 'user_id': 8655964.0}}