assign four array items to one int

I have array of four items: a[0][0], a[0][1], a[0][2], a[0][3]
I want to assign these four to one int:
no problem to assign two items like
array[ row ][ column ] = id;
is there a possibility to assign four?
testArray[ 0 ][ 0 ] = a;
testArray[ 0 ][ 1 ] = b;
testArray[ 0 ][ 2 ] = c;
testArray[ 0 ][ 3 ] = d;
testArray[0] = id;

If can not use int, we can use any other type as holder...
Last edited on
I'm sorry, but it's very difficult to understand your question.

I have array of four items: a[0][0], a[0][1], a[0][2], a[0][3]

It looks like a might be an array where each array element is an array that contains four elements (i.e. an array of arrays, also known as a multidimensional array or 2D array).

no problem to assign two items like
array[ row ][ column ] = id;

array[row][column] is just one item/value/object/whatever you call it.

is there a possibility to assign four?
testArray[ 0 ][ 0 ] = a;
testArray[ 0 ][ 1 ] = b;
testArray[ 0 ][ 2 ] = c;
testArray[ 0 ][ 3 ] = d;

Isn't this code already doing four assignments?

testArray[0] = id;

This won't work if testArray is an array of arrays because you cannot assign an integer to an array.
Last edited on
Its just 2d array, initialized int testArray[ 13 ][ 13 ] = {{ 0 }};
Is there any way to make something alike?
Last edited on
So what do you want to do with it?
impetus, can you give an example with real numbers, not 'a', 'b', 'c', 'd', 'id'. Are you trying to individually manipulate each byte of an int separately? Or are you just trying to do some shorthand to assign all inner indices at once?

If you're trying to do type punning, note that C is lenient on this sort of thing, but C++ is (at least, in terms of the standard itself) more strict here. Something like std::copy or memcpy could work as a one-liner.
you can use a block copy like memcpy to move 4 consecutive ints in a 2d array into a working 1d 4 location array. This works for ints and simple things, but its kinda Cish. If that has any relevance to what you wanted.
you can even exploit a 1 byte aligned struct (or whatever words you want for no padding/alignment) to do the same, if you want to name them a,b,c,d.
struct duh
{
int a,b,c,d;
};
duh x;
memcpy(x,array[row],4*sizeof(int));
cout << x.a; //is array[row][0];
cout << x.b; //array[row][1];

and so on. Basic C style memory tricks and smoke & mirrors. But its not terribly great idea or great code.
most compilers have a pragma or something to align as you need, or a global setting (the pragmas can get current, change it, restore current back pattern).
Last edited on
I did it for combinations, to get all possible combinatios, excluding duplicates...
This one is solved. Thanks for posting, guys!
Topic archived. No new replies allowed.