Make an Array "On the Fly"

Hi,
In the Intel Galileo Arduino IDE, there is a function, SPI.transferBuffer, which requires an array of the data you want to send. What I have been doing is this:

byte xBuffer = {x, x};
SPI.transferBuffer(xBuffer, NULL, 2);

However, I'd like to save some time/space and was wondering if I can sort of create the array from the two bytes in the function, like one can use a byte cast, char cast, etc.
Is this possible?
Thanks

Heh. I just posted this (untested) code on another forum. It looks close to what you want.
(of course, it was an example of "ugly" code, sort of...)

uint8_t avrisp_transaction(uint8_t a, uint8_t b, uint8_t c, uint8_t d) {
   uint8_t outdatat[4];
   uint8_t indata[4];
   outdata[0] = a;
   outdata[1] = b;
   outdata[2] = c;
   outdata[3] = d;
   spiDRV->ARM_SPI_Transfer(outdata, indata, 4);
   while (spiDRV->ARM_SPI_GetDataCount() < 4)
     {  /* spin */ }
   return indata[3];  // Last byte input is result.
}

Um it is? The SPI.transferBuffer bit is irrelevant, I just want an way to cast an array temporarily. I don't want to have to actually declare a separate array at all.

indeed:
Hi,
In the Intel Galileo Arduino IDE, there is a function, SPI.transferBuffer, which requires an array of the data you want to send. What I have been doing is this:

byte xBuffer = {x, x};

SPI.transferBuffer(xBuffer, NULL, 2);



However, I'd like to save some time/space and was wondering if I can sort of create the array from the two bytes in the function, like one can use a byte cast, char cast, etc.
Is this possible?
Thanks

Which 2 bytes in the code snippet you posted do you want to create the array from. I assume the fact that there is no array in the code snippet is an error of transcription.

As usual, seeing a full example of what you are doing now would be helpful in suggesting possible solutions..

Right!
What it should have been was

byte xBuffer[] = {x, x}

I'll edit that.
The reason I want to do this instead of just 2 SPI.transfers is that I heard it's quicker to use SPI.transferBuffer for multiple consecutive transfers (since the Galileo's SPI is pretty slow from the Arduino emulator). Whether that is true or not is sort of irrelevant since I'd like to know if what I'm asking is possible anyway.

You can make the array static, then write to each element.

Or mask the bytes together, however testing would have to prove any benefit ( or loss ).

Why not a C++ approach then:

struct Combine2{
  operator char*(){ return ( char* ) this; }
  byte a,b;
};

//...

SPI.transferBuffer( (Combine2){ x, y }, NULL, 2 );

If you do not like the compound literal, you can provide a constructor.

Well I could do that, but that approach would probably lead to a loss, and requires more code anyway...
Is there no way to do it inline?

would probably lead to a loss

Really,
that is inline, it constructs a temporary object. So, you want to do something, but write no code for it...

Code size != Compiled size.

If you are aiming for less lines just put it all on one:

  struct Combine2{ operator char*(){ return ( char* ) this; } byte a,b; };SPI.transferBuffer( (Combine2){ x, y }, NULL, 2 );

indeed:
that approach would probably lead to a loss, and requires more code anyway...
Is there no way to do it inline?

I don't understand your objections. The approach you showed is a nice simple way to do it, and I don't see what 'loss' you're referring to or why the amount of code is excessive - it's a single line of code a couple of dozen characters long; how much shorter do you think you're going to get it?

It would be possible to work out the location of the two arguments in the calling frame and pass the address and byte count to your sending function, but it would be a horrible approach to take and just to avoid two byte assignments I don't see the justification.

Alright yes I know that would work, but when I said inline what I meant was like you can do with other types:

char var = 'h';
function((byte)var);

Which I guess technically does what I asked, but I just want to know if there's a default, built-in thing like the above for arrays, that requires no additional code, just like the above requires no additional code other than the standard libraries.
Anyway thank you for your input.

but I just want to know if there's a default, built-in thing like the above for arrays

No, there isn't. Think about what an array IS. It is a contiguous block of memory. A variable here and a variable there are not contiguous. Not amount of lying to the compiler (which is what casting is) will change that.

Ok, that's what I was looking for. Thank you.