Does anyone know how to set the binary value of a char pointer? (ikr
)
Basically what I would like to do is use an integer to set the binary value of a char* and increment the value of the integer thus changing the char* value progressively. I understand that the application of this may seem totally wacko, but I am in a very unique situation who's explanation would go far beyond satisfying your curiosity and trying my writer's-patience.
Assign the value that you want cast into a char*?
foo = (char*)55
Not sure if that would work, though.
If it compiles...
char* whatever;
char buffer[2];
void setup( void )
{
whatever = &buffer[0];
}
void loop( void )
{
++(* (int*) whatever);
}
marco_c, I tried typecasting, but to no avail.
Coding Badly, that worked! Thanks!
Basically what I would like to do is use an integer to set the binary value of a char* and increment the value of the integer thus changing the char* value progressively
A dereferenced char * pointer is a short (8bit) form of integer, so you can do math on it directly.
The following sketch will print “Aest Best Cest … Zest”
char *s = "sest";
void setup() {
serial.Begin(9600);
*s = 65; // Note 65 == 'A'
serial.Println(s);
}
void loop() {
if (*s < 'Z') { // Check whether we reached the end of the alphabet
*s += 1; // Increment the first letter of the word to next value
Serial.println(s);
}
}
You can also access pointers as arrays. Adding “s[1] += 1;” to the above at some point would result in “Afst”
Note that *s++ increments the pointer rather than the thing pointed to. You could say “(*s)++;”
fuzzball27:
Does anyone know how to set the binary value of a char pointer? (ikr![]()
)
The pointer holds an address in memory. You can change the pointer with math. If it is a byte pointer then adding 1 to that pointer will address the next byte, subtracting 1 gets the one before. And if it's an int pointer then the address moves by ints. The best way to set the address is to make it = myarray, the name without brackets that points to myarray[0]. Your compiler handles where that actually is in memory while it's in scope.
[/quote]Basically what I would like to do is use an integer to set the binary value of a char* and increment the value of the integer thus changing the char* value progressively. I understand that the application of this may seem totally wacko, but I am in a very unique situation who's explanation would go far beyond satisfying your curiosity and trying my writer's-patience.
[/quote]
Read up on pointers and pointer math. Also on pre-increment and post-increment operators.