in my c++ program I want to pass an array to a function and print the members of that array to console.
now I got into two problems:
int main()
{
unsigned char numbers[8] = { 1,2,3,4,5,6,7,8 };
for (auto i = 0; i < sizeof(numbers); i++)
{
std::cout << numbers[i] << "\n"; // First Problem: Here i get
}
logger(numbers);
}
passing the numbers
to logger
defined as void logger(unsigned char data[])
cause the type change to unsigned char *
so there is no way to iterate over the array as the size is unknown.
my goal also is to pass any sized arrays but assuming that the size of an array is always 8, I changed the signature to
logger(&numbers)
void logger(unsigned char(*data)[8])
{
for (auto i = 0; i < sizeof(*data); i++)
{
std::cout << *(data[i]) << "\n";
}
}
iterating over data
has the first problem and output is ``
so the questions are;
- why do I get a weird ASCII character at
cout
. - how should we deal passing an array to another function and iterate over it, I searched alot but found no solution