Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type casting char pointer to integer pointer

So I saw a few example on how the endianness of an architecture could be found. Let's say we have an integer pointer that points to an int data type. And let's say the int value is 0x010A0B12. In a little endian architecture, the least significant byte, i.e, 12, will be stored in the lowest memory address, right? So the lowest byte in a 4-byte integer will be 12.

Now, on to the check. If we declare a char pointer p, and type cast the integer pointer to a char * and store it in p, and print the dereferenced value of p, we will get a clue on the endianness of the architecture. If it's 12, we're little endian; 01 signifies big endian. This sounds really neat...

int a = 0x010A0B12;
int *i = &a;
char *p = (char*)i; 
printf("%d",*p); // prints the decimal equivalent of 12h!

Couple of questions here, really. Since pointers are strongly typed, shouldn't a character pointer strictly point to a char data type? And what's up with printing with %d? Shouldn't we rather print with %c, for character?

like image 802
nirvanaswap Avatar asked Feb 19 '16 02:02

nirvanaswap


People also ask

What happens when you cast a char pointer to an int pointer?

When casting character pointer to integer pointer, integer pointer holds some weird value, no where reasonably related to char or char ascii code. But while printing casted variable with '%c', it prints correct char value. Printing with '%d' gives some unknown numbers.

Can you assign a pointer to a char to a pointer to int?

A pointer is a particular variable that stores the memory address where another variable begins. Doesnt matter if the variable is a int or a char, if the first bit has the same position in the memory, then a pointer to that variable will look the same.

Is it possible to cast a pointer to float as a pointer to integer?

pointer is a address, and int float are stored in memory in different format. so you can not cast it.

Can a pointer be char?

A character pointer is again a pointer like the pointers to other types in C. Here ptr is pointer to a character. A point to note here is - pointer to a character is different from the pointer to a string. In C, strings are defined as an array of characters.


1 Answers

Since pointers are strongly typed, shouldn't a character pointer strictly point to a char data type?

C has a rule that any pointer can be safely converted to char* and to void*. Converting an int* to char*, therefore, is allowed, and it is also portable. The pointer would be pointing to the initial byte of your int's internal representation.

Shouldn't we rather print with %c, for character?

Another thing is in play here: variable-length argument list of printf. When you pass a char to an untyped parameter of printf, the default conversion applies: char gets converted to int. That is why %d format takes the number just fine, and prints it out as you expect.

You could use %c too. The code that processes %c specifier reads the argument as an int, and then converts it to a char. 0x12 is a special character, though, so you would not see a uniform printout for it.

like image 179
Sergey Kalinichenko Avatar answered Sep 22 '22 01:09

Sergey Kalinichenko