0

I am trying to set a char array with the first 2 chars equal to 698 in binary, the 3rd and 4th chars a dynamic number under 180 and the rest a predefined set of 150 characters followed by an eof-is there a simple way to alter char arrays so I could, in example, do this:

char arr[155];
arr [0-1] = uint_16(698);
arr[2-3] = uint_16(178);
arr[4-154] = (another message);

I am new to c++ and am not experienced with these things.

M.M
  • 138,810
  • 21
  • 208
  • 365
dashiz
  • 41
  • 4

2 Answers2

1

C++ does not provide language-level mechanisms for array slicing. There are mechanisms you can use to do what you are trying to achieve, though.

You can be clumsy and dangerous and use aliasing (example removed)

But a simpler and cleaner way to do this would be simply to declare a struct:

#include <cstdint>

struct Replacement {
    uint16_t first = 698;
    uint16_t second = 178;
    char message[150] = { /*... values ...*/ };
};

int main() {
    Replacement r;
    // use 'r' instead of 'arr'.
}

If you want to make absolutely sure there's no padding in the struct

#pragma pack(push, 1)
struct Replacement {
    uint16_t first = 698;
    uint16_t second = 178;
    char message[150] = { /*... values ...*/ };
};
#pragma pack(pop)
kfsone
  • 23,617
  • 2
  • 42
  • 74
  • I don't see anything in this question that calls for a `reinterpret_cast` – caps Sep 16 '16 at 03:25
  • @caps removed it. – kfsone Sep 16 '16 at 03:29
  • A C-style cast is even worse. What you had before was better. My point is that any solution that involves a cast seems to be looking in the wrong direction. – caps Sep 16 '16 at 03:30
  • @caps I'm going from the fact that he's originally trying to put it into a char array and using the term "message". It makes it look like he's trying to build a byte-buffer for something that takes a `char*` pointer. – kfsone Sep 16 '16 at 03:31
  • @M.M Aliasing version removed, – kfsone Sep 16 '16 at 03:34
  • I still need to concatenate first, second, message and '\o' into one char array though: how would I do that? – dashiz Sep 16 '16 at 03:39
  • @dashiz it's already in a contiguos array of bytes, all you'd have to do is take the address of `r` and cast it to `char*` or `const char*`, e.g. `(char*)&r`. – kfsone Sep 16 '16 at 03:41
  • That isn't working because I would need to set the 'message' and 'second' values. HelperStru halp; halp.second = incheckl; strcpy(halp.message,buffer); std:: cout << "halp: " << (char *)&halp << ", sizeof(halp): " << sizeof((char*)&halp) << std::end; results in 'halp: ?, sizeof(halp): 8' – dashiz Sep 16 '16 at 03:53
  • @dashiz What do you expect the output to be? It may be better to close this question and open another describing the end-goal you're trying to achieve. I'm interpreting it as though you want to create a binary buffer to write as binary data to a socket or file. – kfsone Sep 16 '16 at 03:57
  • I take it back. Putting a 2-byte number in a `char` array will require some kind of cast. – caps Sep 16 '16 at 04:16
-4

For 698 and 178, here is a trick. int *a = (int *)arr; *a = 678; a = (int *)(arr + 2); *a = 178; It uses overflow to fill char array so the order of assignment must be taken into consideration.

As for another message, if you save it in another char array, use strcpy(better strncpy) to copy it into (arr + 4). Or read the message into (arr + 4) directly.

Ben Lee
  • 102
  • 1
  • 1
  • 9
  • 2
    This causes undefined behaviour – M.M Sep 16 '16 at 03:03
  • @M.M could you please talk more about the undefined behaviour? To this question, after 155 bytes are allocated, `*a=678` changes 0-3 and `*a=178` changes 2-5. After copy the message into 4-154, the task is finished. – Ben Lee Sep 16 '16 at 03:10
  • @BenLee an `int` lvalue may only be used to modify `int` or `unsigned int` objects. [See here](http://stackoverflow.com/a/7005988/1505939) – M.M Sep 16 '16 at 03:12