Your latest edit is incorrectly initializing the array.
int main(int argc, char *argv[]){
int8_t v[4] = {'0','1','2','3'}; // <- This is wrong!
int y;
y = (int) v[1]*3;
std::cout << v[1] << " " << y << std::endl;
return 0;
}
Numbers in single quotes are ASCII characters whose numeric values don't match the actual number in the quotes.
Please modify your code like this and see if it helps:
int main(int argc, char *argv[]){
int8_t v[4] = {0, 1, 2, 3}; // <- Remove the single quotes
int y;
y = (int) v[1]*3;
std::cout << v[1] << " " << y << std::endl;
return 0;
}
Here is a link to ideone
Follow up question (from comments):
I never declare the actual array, I import it directly from a .txt file. When I do this I get the same result as if I had declared it with the quotes. Is there a way to work around this, converting it from ascii code to the desired numeric value?
Yes. You can use the atoi or stoi functions to convert a number that is represented as text to an integer. You should also take a look at this link discussing how to use stream operators to achieve the same.
However, converting the numbers from strings is pretty easy so I'll give you an example:
#include <iostream>
#include <string.h>
int getNumberFromString(const char* numberString)
{
int number = 0;
int i;
int stringLength = strlen(numberString);
for (i = 0; i < stringLength; ++i)
{
number *= 10;
number += numberString[i] - '0';
}
return number;
}
int main()
{
const char* numberSeventyNineString = "79"; // Equivalent to { '7', '9', '\0' }
int numberSeventyNine;
numberSeventyNine = getNumberFromString(numberSeventyNineString);
std::cout << "Number (" << numberSeventyNineString << ") as int is: " << numberSeventyNine << std::endl;
return 0;
}
And here's the ideone link
Note: The line const char* numberSeventyNineString = "79"; // Equivalent to { '7', '9', '\0' }
has a comment saying that this is equivalent to an array, for a little more detail please see this answer