In general the program has undefined behavior . According to the C Standard(6.3.2.3 Pointers)
6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
For example sizeof( char * ) can be equal to 8 while sizeof( int ) can be equal to 4. That is an object of the type int can be unable to store a value of a pointer.
Instead of the type int in this declaration
int num = character;
you should use type intptr_t declared in the header <stdint.h>
For example
#include <stdint.h> //... intptr_t num = ( intptr_t )character;
So now the variable num contains the address of the first character of the string literal "abcd".
And after this declaration
intptr_t *pnum = #
the pointer pnum has the address of the variable num.
Now to output the first character of the string literal you have at first to dereference the pointer pnum that to get the value stored in the variable num. This value is a representation of the address of the first character of the string literal. You need to cast it to the type char * and again to derefercen it.
Below is a demonstrative program that shows how it can be achieved. If you will not dereference the pointer then the whole string literal will be outputted.
#include <stdio.h> #include <stdint.h> int main(void) { char *character = "abcd"; printf( "%d\n", *character); intptr_t num = ( intptr_t )character; intptr_t *pnum = # printf( "%s\n", ( char * )*pnum ); printf( "%d\n", *( char * )*pnum ); return 0; }
The program output is
97 abcd 97
int num = *character; int * pnum = # printf("%d \n", * pnum);printf("%s \n", pnum);and better, you usechar * pnum;and a cast:*pnum = (char *) #