-5

If I have the following code example:

#include <iostream>

using namespace std;


unsigned char Receive(void){
    unsigned char q[4] = {0x0B, 0x0B, 0x0B, 0x0B};
    return q;
}

void Transmit(){
    unsigned char a[4];
    a = Receive();
}

int main()
{
    Transmit();


    return 0;
}

Then why can't I assign the return value of Receive(void) to the variable q when they both are unsigned char?

EDIT(see my comment below):

unsigned char Receive(void){
    array<unsigned char, 4> q{{0x0B, 0x0B, 0x0B, 0x0B}}
    return q;
}

void Transmit(){
    array<unsigned char, 4> a;
    a = Receive;
}
  • 5
    `a` is an array. `Receive` returns an `unsigned char`, not an array. – CoderCharmander Dec 05 '19 at 14:14
  • 3
    `q` is **not** an `unsigned char` in `Receive()`. – PaulMcKenzie Dec 05 '19 at 14:14
  • 1
    If you want your code to work as you believe it should, use `std::array`, not plain arrays. – PaulMcKenzie Dec 05 '19 at 14:18
  • @PaulMcKenzie Like in my edit? It doesn't work.. – user164324 Dec 05 '19 at 14:40
  • You are returning an array. Thus your function return type should be a `std::array`. Or are you confused as to what you should really be returning? Are you to return one number, or an array of numbers? – PaulMcKenzie Dec 05 '19 at 14:41
  • A collection of four numbers is not the same kind of thing as one number. There is a list of good books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Dec 05 '19 at 14:43
  • I want to return an array of numbers. – user164324 Dec 05 '19 at 14:59
  • Slow down. Take a moment. Look at the code. Think about what you're returning, and what the return type should be. Take your time. – Lightness Races in Orbit Dec 05 '19 at 15:03
  • `Receive()` generates a warning: **error: invalid conversion from 'unsigned char*' to 'unsigned char' [-fpermissive]**. Do you have enabled the compiler's warnings (such as -Wall -Wextra for gcc & clang)? – Bktero Dec 05 '19 at 15:28
  • You need to understand the difference between a *single* value and an array of values. The function return type is a *single* value, but the code returns many values (an array). – Thomas Matthews Dec 05 '19 at 16:10

2 Answers2

1

Why can't I assign [result of] a function to a variable?

Because the types don't match. The function returns an unsigned char, while the type of the variable is unsigned char [4]. Latter is not assignable with the former. In fact, nothing can be assigned to an array.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

You really don't want to return an array. Returning an array causes the compiler to make copies of the array (which slows things down).

The preferred method is to pass a pointer:

unsigned char const *  Receive()
{
    static const unsigned char values[] = {0xf0, 0x0d};
    return &values[0];
}

The problem here is that the capacity of the array is not returned. A pointer to the first element is returned, but that is all.

You could pass an array and have the compiler copy all the elements:

std::array<unsigned char, 4> Receive()
{
    std::array<unsigned char, 4> q{{0x0B, 0x0B, 0x0B, 0x0B}};
    return q;
}

Another method is to pass a std::vector by reference:

void Receive(std::vector<uint8_t>& container)
{
   container.push_back(0x08);
   container.push_back(0x0B);
   //...
}

void Function()
{
    std::vector<uint8_t> data;
    Receive(data);
    //...
}

Passing by reference eliminates the need to copy the data structure. The function modifies the caller's variable directly.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154