6 ms·
I've made this mistake in the past. In C++ I used a reference to fix it like so: #include <cassert> typedef char array_xyz_type[42]; void test(ar
by anthay 11y ago
I've made this mistake in the past. In C++ I used a reference to fix it like so:
#include <cassert>
typedef char array_xyz_type[42];
void test(array_xyz_type & a, array_xyz_type b, char c[42], char (&d)[42])
{
assert(sizeof(a) == 42);
assert(sizeof(b) == 4);
assert(sizeof(c) == 4);
assert(sizeof(d) == 42);
}
int main()
{
array_xyz_type x = {1,2,3};
test(x, x, x, x);
}
I'd add these comments:
1. The reference makes the code somewhat less flexible in that you can't call the function with anything other than a char[42] type, e.g. test(new char[42]...) won't compile. (This may or may not be what you want.)
2. As Linus said, don't ignore compiler warnings.
3. Write some code, then watch every line execute in the debugger so you see if what you wrote is what you thought you wrote. Repeat.