I am vastly confused. I'm having a headache. I'm new to C.
#include<stdio.h>
int main()
{
int i = 257;
int *iPtr = &i;
printf("val 1: %d \t val 2: %d \n\n",*( (char*)iPtr ), *( (char*)iPtr+1) );
int iptr_alias = iPtr;
int and_val = iptr_alias & 255;
printf("Value of iPtr (aka address of i): %d \n\n", iPtr);
printf("Value at location pointed to by iPtr using %%c: %c \n\n",*iPtr); //gives weird character
int f = 257;
int *fptr = &f;
printf("char starred fptr: %d \t charred 257: %d \n\n",*((char*)fptr), ((char)257) );
// i know the above is obvious.
system("PAUSE");
}
My questions:
1. Apparently *( (char*) iPtr )
= 257 & 255
= 1 (bitwise and operation).
And (char)*iPtr
is the same also . But it doesn't print 1
if i use %c
modifier. Why?
2. And why *( (char*) iPtr+1 )
= 1 ??
I'm so confused with all this (which i wrote myself to clear confusion, but it worked otherwise..)
((char)257)
gives 1
using %d
or %o or %x for that matter. %c
gives some weird ASCII character
I mean, when i do printf(" %c ", 257)
, then i don't get 1
, instead, I get a weird ASCII character. Why?
In the actual problem, I was supposed to determine what would be printed as val 1
and val 2
.
I maybe overlooking anything stupidly simple, but i'm really feeling confused and tired about this mess. Please help.