I need the BYTE variable someData in my main function because a followup function needs it for data processing.
Your "because" clause is actually a reason why you do not need someData in your main function. You need someData in the follow-up function. The only trick here is that your data has different types. A template can help with that. Have the follow-up function accept a reference to an array as its parameter, and let the template parameter specify the size of this array.
#include <iostream>
#include <string>
using BYTE = unsigned char; // No need to depend on Windows.h
template <size_t size>
void do_work(const BYTE (&someData)[size]) {
std::cout << "SomeData: ";
for(size_t i = 0; i < size; i++) // Corrected limit, not 23
{
printf("%02x ", someData[i]);
}
printf("\n");
}
int main()
{
std::string title;
std::cout << "CHOOSE PARAM: ";
// std::cin >> title;
// Simulate user input:
title = "PARAM2"; std::cout << "\n";
if (title == "PARAM1") {
do_work({ 0x00, 0x87, 0x80, 0x83, 0x10, 0x09, 0x08, 0x00, 0x83, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00 });
}
else if (title == "PARAM2")
{
do_work({ 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xBE, 0x00, 0x40 });
}
else if (title == "PARAM3")
{
do_work({ 0x01, 0x01, 0x16, 0xD5, 0x01, 0xD8, 0x00, 0x00 });
}
}
As long as the follow-up function does not need to change the data, you can pass the brace-enclosed list directly to the function, no need to define a variable in main. There's also no need to write the size of each array, as the compiler will count that for you.
A caveat to the above is that even though the code for do_work is written once, it compiles three times, once for each size. If do_work is large, this might lead unnecessary bloat in your executable. You could get around this by delegating to a function that takes a pointer and a size, which as of a few years ago (C++20) can be in the form of a std::span.
// The real function
void do_work(std::span<const BYTE> someData) {
std::cout << "SomeData: ";
for(size_t i = 0; i < someData.size(); i++)
{
printf("%02x ", someData[i]);
}
printf("\n");
// A range-based loop is also possible:
std::cout << " ";
for(auto& data : someData)
{
printf("%02x ", data);
}
printf("\n");
// A pointer to the data is available via the `data()` member.
[[maybe_unused]] const BYTE* as_pointer = someData.data();
}
// The wrapper to forward const arrays to the real function.
template <size_t size>
void do_work(const BYTE (&someData)[size]) {
do_work(std::span{someData});
}
The small delegating function template helps the compiler go from a braced-enclosed list to a std::span. If you stick to defining variables inside your if statement (presumably because you need non-constant data) then this delegation is not necessary.
std::map<std::string,std::vector>. The reason to use std::vector is that it can be any length). And you processing function can accept a const reference to said std::vector. You should also not "#include <windows.h>" (C++ has <cstdint> + std::uint8_t for bytes). You should not be usingusing namespace std;and usestd::print(C++23).for(int i = 0; i < 23; i++)would do out of bound access, even for the larger array anyway.