I am doing some exercises to figure out how to access values in an array after they are changed with pointers. Can someone point out why the first output does not show the desired output? I am trying to get both cout to print 1234, one by using the new pointer and one by using the position in the array
int main()
{
char myArray[50]={0};
short* sizeOfAlloc=(short*)(myArray+5);
*sizeOfAlloc=1234;
cout << (short*)(myArray+5) <<endl;
cout << *sizeOfAlloc <<endl;
system("pause");
}
char**but you cast it to ashort*. 2 problems: 1. (the main one), you have changed the level of indirection. 2. You have cast fromchartoshort, which you should only do if you really know what you're doing (more likely you want to cast toint16_t)