Switching a Variable's Type

Short post short: Can I switch a variable from one type to another?
e.g.

void setup(){
int x;
String x;
}
void loop(){}

Returns an error.
Is there any way of "deleting" the variable to redefine it or a way of simply changing the type?

Thanks in advance,
Digitalis

You can use 'unions' or pointers to that.

Digitalis:
Short post short: Can I switch a variable from one type to another?

See Arduino - Cast

@luisilva
Looking at a tutorial on pointers, they look potentially helpful.
However, I can't figure out how exactly to redefine a variable as a whole using this method, even if I can change the address of a variable.
Is it possible to set the address to null or something to basically "delete" a variable as far as the actual RAM taken up?

@pantaz
I looked at cast.
Neat stuff, but that only translates the contents.
I need the actual variable as it exists in its space to be redefined.
Sorry for being vague.

Digitalis:
Is it possible to set the address to null or something to basically "delete" a variable as far as the actual RAM taken up?

If you need to delete try the dynamic allocation memory (I don't know if you have libraries for Arduino, but you can check it).

So basically there's no real way of swapping the type without toying about with allocation and pointers, the former of which could potentially cause memory leaks?
That's sort of annoying. :confused:

To explain in detail what I want to do: I want to have a program of sorts to handle allocation. However, I don't know how to go about making one without variable redeclaration of type, which I can't find out how to do.

Different types are different sizes. If you have a byte and later try to write an int to that location, you will overwrite whatever was in the next adjacent memory location.

Dynamic memory allocation with malloc() and free() can be used to store arbitrary numbers of arbitrary variables. But then you need to use more memory to remember where the variable is and what type it is.

This is not what you are asking for, but is in line with the suggestion by luisilva:

union {
  int number;
  char str[20];
} myUnion;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  myUnion.number = 10;
  Serial.print("Integer number = ");
  Serial.println(myUnion.number);
  strcpy(myUnion.str, "Hi Digitalis");
  Serial.print("string str = ");
  Serial.println(myUnion.str);
 
}

void loop() {
  
}

You can write integers into string space, but why would you ever want to do this? It's a train wreck waiting to happen.

@MorganS
I'm guessing it would be possible to store everything as binary, and do things that way. Then technically I could just convert it, avoiding declaring variables other than them. (?)

If the above option fails, I have two other less appealing options:
a) Use MicroSD or EEPROM as if it were RAM
b) Have a predefined amount of each type of variable

I'm guessing it would be possible to store everything as binary

The mcu already does that.

and do things that way.

Whatever that means.

You've just about beaten that poor bush to death. It's time to quit being so vague.

PaulS:
The mcu already does that.
Whatever that means.

You've just about beaten that poor bush to death. It's time to quit being so vague.

I mean as in storing things in actual binary, just 1's and 0's, no abstractions.
Then when it's needed, "convert" the binary without using abstracted variables, e.g. a Byte.
(e.g. B1000 being displayed as 8 on a display via code.)
By doing so I'm guessing I could basically just use this binary as whatever format is needed.
Would this work?

Give an example.

bytes, ints, longs and even strings are actually stored in exactly the same bit pattern.
so the number "9" or even "127" have already the same bit representation, either if they are stored as bytes, ints, or floats or as a character in a string.
They just need different memory space, this is what you actually need to know in order how many bytes are used: for a byte, the next memory cell probably will contain a different variable value, and for 9 as a long you will read all the following 3 bytes as a '0' (sry, edited)
this is 9 as a byte: 00001001
this as a char (actually a unsigned char) in a string: 00001001
this as an int : 00000000 00001001
this as a float: 00000000 00000000 00000000 00001001
(ok, there also is an issue about if first low bytes or first high bytes stored, called "endianess", and note that in a string the NUMBER 9 is totally different from the code of a sign which LOOKS LIKE a '9' when printed.)

so you can convert always a byte into an int or a long, but mostly not vice versa.

Just floats are different coded (from byte 0 to 31: 22 Mantissa, 9 exponent, 1 sign) - but this is not under your control.

An example might take a while to write up, but I'll try.
To give a basic overview before I make some basic code, here's kind of how it would work:

-Declare a ton of bytes.
-Declare a few abstracted variables for temporary work.
-Allocate and overwrite them as needed.
-When needed to display to the user, convert to decimal and use one of the abstracted variables to store it.
-Display it as needed, then clear the temporary abstract variable.

If I were to make "example code" I'd basically have the finished product done. Basically what I want to do is use something like a byte so I can temporarily convert it as needed, then have these large amounts of bytes be allocated at will in the form of a library for managing SRAM.

@ArthurD
Thank you for the information on the size of each type (byte, int, float.)
Does the byte actually come out as binary?
Or if I did "Serial.print(byte_name)" would it just spew it out as decimal?

You are so confused. What you are asking for, is already done by the compiler and I/O subsystem. Except the the "abstracted variables", which I think is just an artifact of your confusion.

Perhaps you are ignorant of pointers? What you are asking for sounds sort of vaguely like what they can do.

aarg:
You are so confused. What you are asking for, is already done by the compiler and I/O subsystem.

I know that.
I’m asking if there’s a way of redefining variable types, or reading out a byte, int, or float as plain binary.

What Serial does is mostly obscured, weird, corrupted, and unpredictable, this is because Arduino made it to an overloaded funktion which outputs different variable types in a different manner.

If I print out strings, I mostly print out strings, and to get them I convert a number into a string before.

sprintf(str, “formatstring”, number);
Serial.print(str);
And this is perhaps what Serial does for numbers and chars by it’s own obscured rules.

In principle this also works for floats, but “intelligently” not on AVR Arduinos by design (of course a feature, not a bug, but IMO actually the HELL of a feature).

That’s just Arduino at it’s best.

But for bytes, ints, longs,… you always already read plain binaries, nothing else, that’s what I already explained above.
I can’t explain it in a better way, so I whish you all the best! :sunglasses:

IDE documentation:

Serial.print(78, BIN) gives "1001110"
Serial.print(78, OCT) gives "116"
Serial.print(78, DEC) gives "78"
Serial.print(78, HEX) gives "4E"
Serial.println(1.23456, 0) gives "1"
Serial.println(1.23456, 2) gives "1.23"
Serial.println(1.23456, 4) gives "1.2346"

aarg:
-snip-

Thank you.
So is it possible to store something as a byte and have it default to “BIN” so I can use it jointly for numbers and, in a somewhat cheaty way, as letters?

I'm asking if there's a way of redefining variable types, or reading out a byte, int, or float as plain binary.

No. A variable's type defines how many bytes it uses in memory and how the value is represented in those bytes.

If you were to change the type of 4 bytes that are a float to long, the data in those 4 bytes would be meaningless as a long.

"Reading out a " is just plain nonsense. The data in a (series of) memory address(es) is in binary. You are free to print the value in whatever base you like. Print an int as base 6 if that makes your heart go pitter-patter for some bizarre reason. Print it in base 2 if that is what you need to do.