I was trying to access single bytes of an int value via the code below. My problem is that whenever I try to remove long int i=0;
from the code, it gives me a segmentation fault. Is there any reason this happens? I am not using I anywhere in the code.
// Online C++ compiler to run C++ program online
#include <iostream>
int main() {
// Write C++ code here
unsigned int* a;
unsigned char* b1;
unsigned char* b2;
unsigned char* b3;
unsigned char* b4;
*a= 4294967295; //set to max val (4 bytes)
//*************************
long int i=0;//Q. Why long int/long long int?
//*************************
b1 = (unsigned char*)(a);
b2 = b1+(long long int)1;
b3 = b1+(long long int)2;
b4 = b1+(long long int)3;
std::cout <<*a<<" "<<(int)*b1<<" "<<(int)*b2<<" "<<(int)*b3<<" "<<(int)*b4<<std::endl;
return 0;
}