Reputation: 133
I'm trying to print/return the array listum in code below, but receive error messages about invalid conversions. After a little research it seems that printing arrays all at once is not possible in C++, but must be printed in a for loop of individual letters. I would simply do that but I also read that one return command takes the computer straight out of the function so I don't think I would be able to return the individual letters.
Any guidance on how to best do this is very much appreciated and I ask that you keep in mind that I'm a mediocre python coder at best and do very little C++.
void setup()
{
Serial.begin(9600);
Serial.print(printletters());
}
void loop()
{
}
void printletters()
{
String wordsOfA = "BooffrOM";
int listum[]= {};
int listnumber=0;
char listy[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','@','#','$','%'};
for (int k=0; k<wordsOfA.length(); k+=1) {
char let = toupper(wordsOfA[k]);
int wantedpos;
for (int j=0; j<40;++j) {
if (let == listy[j]) {
wantedpos = j;
listum[listnumber]=wantedpos;
listnumber=listnumber+1;
break;
}
}
}
Serial.print(listum);
return listum;
}
PS I removed a lot of unrelated sections of the code for readability, if you feel that these sections may be part of the problem, please comment and I will post the full code.
Upvotes: 0
Views: 103
Reputation: 2481
In C/C++ one does not simply return an array from a function.
When you allocate an array inside a function, you cannot return a pointer to the first element in that array, since when the function returns, all local variables allocated inside that function are wiped clean.
int* printLetters() {
int listum[];
return listum;
}
When you do this, a pointer to the first element in the array listum
is returned from the function. If you know how many elements there are in the array, you can loop through and offset that returned pointer to print each element.
You could do this if it were not for the caveat that whenever you return from a function any stack allocated local variables are destroyed or wiped clean, so you are returning an invalid pointer. This is because these variables are stored on the function call stack. When a new function is called, the variables are pushed on top of the stack, and when that function is returned, the variables are popped off the stack.
There are two main ways of returning an array from a function. First one, is heap allocating a dynamic array, using malloc()
or new
. The heap is a region of memory outside of the stack, so it is possible to manually allocate a variable inside the heap, but you would have to also manually deallocate that variable otherwise you get a memory leak.
int* returnArray() {
int* arrayPointer = malloc(3 * sizeof(int));
arrayPointer[0] = 42;
arrayPointer[1] = 32;
arrayPointer[2] = 74;
return arrayPointer;
}
int main() {
int* returnedArray = returnArray();
for (int i = 0; i < 3; ++i) {
printf("%i\n", returnedArray[i]);
}
free(returnedArray);
}
In this example we are manually creating three integers in the heap using malloc()
, then fill the allocated integers, and returning a pointer to the first integer. Then in the main function we loop through these three integers, and print each element. Then we need to manually free these three integers, because we manually allocated them.
The second way is to create an array in the calling function, and pass a pointer to that array into the called function.
void returnArray(int(*arrayPointer)[3]) {
(*arrayPointer)[0] = 42;
(*arrayPointer)[1] = 32;
(*arrayPointer)[2] = 74;
}
int main() {
int myArray[3];
returnArray(&myArray);
for (int i = 0; i < 3; ++i) {
printf("%i\n", myArray[i]);
}
}
Here we do not need to free the array because it is automatically allocated in the calling function. We did not manually allocate it.
If that doesn't make sense, perhaps you can watch the following videos:
I would recommend you to watch this playlist:
Upvotes: 2
Reputation: 134
if you want to 'return' basically a string from the function, you would have to dynamically allocate a block of memory, fill it with letters and return an address of that block. But it's actually better to provide a string buffer pointer to the function, so the function would copy the letters to that buffer. And the buffer itself could be allocated dynamically, or statically
there's some code:
#include <string.h>
int CopyString(char* out_buf, int buf_size) {
const char* str = "Hello, world!";
int write_count = strlen(str);
if (write_count > buf_size - 1)
write_count = buf_size - 1;
for (int i = 0; i < write_count; i++) {
out_buf[i] = str[i];
}
out_buf[write_count] = '\0';
return write_count;
}
// how to use
int main() {
char buffer[256];
int string_length = CopyString(buffer, 256);
}
Upvotes: 1