2

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;

  1. why do I get a weird ASCII character at cout.
  2. how should we deal passing an array to another function and iterate over it, I searched alot but found no solution
Tim Klein
  • 2,538
  • 15
  • 19
Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123

3 Answers3

6

The problem lies in the contents of your array:

unsigned char numbers[8] = { 1,2,3,4,5,6,7,8 };

Those get interpreted as character codes (because of the array element type)., not literal values. Most probably the character mapping used is ASCII, and characters 1 through 8 aren't printable.

To obtain the character value representing 1, you'd need to write a character literal '1'. If your intended to store and treat them as numbers, you could either change the type of the array to int[8], or cast them when printing:

std::cout << static_cast<int>(numbers[i]) << "\n";

As a side not, if you intended to use characters, you should change the type to char.


To solve passing the arrays of arbitrary size, either use a template and pass a reference to std::array, or simply use a vector.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
3

You cannot pass an array to a function in C++. There are several ways around this

1) Use vectors instead of arrays

2) Pass a reference to the array (this only works with a fixed size array)

3) Pass a pointer to the first element of the array (this requires that you pass the size as a seperate parameter).

Here's how you do all three

1) use vectors

#include <vector>

std::vector<unsigned char>{1,2,3,4,5,6,7,8}:
logger(numbers);

void logger(const vector<unsigned char>& data)
{
  for (auto i = 0; i < data.size(); i++)
  {
    std::cout << (unsigned)data[i] << "\n";
  }
}

2) use a reference

unsigned char numbers[8] = { 1,2,3,4,5,6,7,8 };
logger(numbers);

void logger(unsigned char (&data)[8])
{
  for (auto i = 0; i < 8; i++)
  {
    std::cout << (unsigned)data[i] << "\n";
  }
}

3) use a pointer

unsigned char numbers[8] = { 1,2,3,4,5,6,7,8 };
logger(numbers, 8);

void logger(unsigned char *data, size_t size)
{
  for (auto i = 0; i < size; i++)
  {
    std::cout << (unsigned)data[i] << "\n";
  }
}

vectors are the best solution. C++ has proper data structures as standard, use them.

As has already been explained your printing problems are due to the special rules for printing characters, just cast to unsigned before printing.

No code has been tested (or even compiled).

john
  • 85,011
  • 4
  • 57
  • 81
1

For your first problem use:

 int arr_size = sizeof(numbers)/sizeof(numbers[0]);
Vijay
  • 65,327
  • 90
  • 227
  • 319