Is there any way to return an array from a function? More specifically, I've created this function:
char bin[8];
for(int i = 7; i >= 0; i--)
{
int ascii='a';
if(2^i-ascii >= 0)
{
bin[i]='1';
ascii=2^i-ascii;
}
else
{
bin[i]='0';
}
}
and I need a way to return bin[]
.
I think your best bet is to use a vector. It can function in many ways like an array and has several upsides (length stored with type, automatic memory management).
you need to pass array bin as an argument in your function. array always pass by address, therefore you dont need to return any value. it will automatically show you all changes in your main program
I think that everyone else answered this one... use a container instead of an array. Here's the
std::string
version:I'm not really sure if
2^i-ascii
is want you want or not. This will be parsed as(2 ^ (i - ascii))
which is a little strange.