1

I wasn't able to find a correct explanation on how to solve my problem.

I have a uint8_t array and I'm able to define it if I use this notation:

uint8_t array[] = {0x58, 0x01, 0x11, 0x00, 0x00, 0x00, 0x10, 0x7A};

However, if I use this other notation:

uint8_t array[8];
array[] = {0x58, 0x01, 0x11, 0x00, 0x00, 0x00, 0x10, 0x7A}

the compiler gives me an error:

error: expected primary-expression before ‘]’ token

How can I define my array later in my code after having initialized it?

EDIT 1:

I need to define the array inside the switch case like this:

uint8_t array[8];
    switch(relay_on){
    case 16: array[] = {0x58, 0x01, 0x12, 0x00, 0x00, 0x00, 0x10, 0x7B}; // 
    break;
    }

and I need to use the array outside the switch. The array is not available outside the switch if I declare it inside the switch case.

EDIT2: use with std::array

   std::array<uint8_t, 8> command1;
    std::array<uint8_t, 8> command2;

    switch(relay_on){
    case 16: command1 = {0x58, 0x01, 0x12, 0x00, 0x00, 0x00, 0x10, 0x7B}; // switch on the relay 16
    break;
    }

    switch(relay_off){
    case 16: command2 = {0x58, 0x01, 0x11, 0x00, 0x00, 0x00, 0x10, 0x7A}; // switch off the relay 16
    break;
    }
int bytes_to_send1 = sizeof(command1);
    int bytes_to_send2 = sizeof(command2);
    int bytes_sent1 = 0;
    int bytes_sent2 = 0;

    do
    {
        n = send(sockfd, command1 + bytes_sent1, bytes_to_send1 - bytes_sent1, 0);
        if ( n < 0 )
        {
            cerr << "Error writing to socket!" << strerror(errno) << endl;
            close(sockfd);
        }
        bytes_sent1 += n;
    }
    while (bytes_sent1 < bytes_to_send1);

if I use the std::array notation, i get this error:

error: no match for ‘operator+’ (operand types are ‘std::array<unsigned char, 8>’ and ‘int’)
n = send(sockfd, command2 + bytes_sent2, bytes_to_send2 - bytes_sent2, 0);
Marcus Barnet
  • 2,083
  • 6
  • 28
  • 36
  • 3
    You can't, at least not without a loop. Are you using C or C++? C++ gives you `std::array` which is like an array, but "better" and supports these mechanics. – NathanOliver Oct 22 '19 at 16:38
  • 2
    Good dup target but use `std::array` instead of `vector`. – Hatted Rooster Oct 22 '19 at 16:39
  • I edited my first topic in order to add the switch case. I need to declare the array outside it and then to define it inside the switch case; then I need to use the array outside later in the code. – Marcus Barnet Oct 22 '19 at 16:48
  • 1
    Use a `std::array`. That would make your code look like `std::array command1; switch(relay_on){ case 16: command1 = {0x58, 0x01, 0x12, 0x00, 0x00, 0x00, 0x10, 0x7B}; // break; }` – NathanOliver Oct 22 '19 at 16:48
  • 1
    as an alternative, you can use `memcpy` – Amadeus Oct 22 '19 at 16:50
  • Thank you, @NathanOliver, it compiles but then it gives me an error as I reported in my first topic. The error is generated when I try to execute command1 + bytes_sent1 – Marcus Barnet Oct 22 '19 at 16:58
  • That's a completely different question, and you just need to read the chapter in your book about how to use `std::array` – Lightness Races in Orbit Oct 22 '19 at 17:03
  • I'm not a student so I have no book, I searched for std::array on google, but I found nothing related to my current problem. – Marcus Barnet Oct 22 '19 at 17:04
  • 1
    @MarcusBarnet A good start is: https://en.cppreference.com/w/cpp/container/array there you can look at this too https://en.cppreference.com/w/cpp/container/array/data – Amadeus Oct 22 '19 at 17:06
  • @Amadeus should I use this notation: command1.data() + bytes_sent1 ? It gives me no error, but does this make sense? – Marcus Barnet Oct 22 '19 at 17:08
  • @MarcusBarnet `send` is a C API, so, you need to provide data on that format. But now, as LightnessRacesInOrbit said, this is another question. For a more complete answer, you need to post a question – Amadeus Oct 22 '19 at 17:11

0 Answers0