c language + simple question regarding memory addresses and ASCII characters

Just a simple question (which may seem silly so bear with me) that arose in my mind the other day. Do ASCII characters by themselves (e.g. /n, 0, a) have an actual memory address ?

My question arises, because Im aware that each time I create and initalise a pointer like this for example

int *ptr = 5;

I always get a null pointer error.

many thanks

Yes they do. Your declaration creates a pointer but does not constrain it to a variable of type int. Does your code compile okay and on what compiler?

For the code I spoke of in my first post,

int *ptr=5;

the compiler (I am using gcc btw), produced one warning saying that a pointer from integer was made without a necessary typecast. (shamrock warned me of this, so this gcc output was probably expected).

I tried this

char *str="helloworld\n";
printf("string value is %s\n",*str)

and found the program compiled fine, but at runtime I got a segmentation fault error. I also found that in the first code segment, if you ignore the compiler warning and run the program straight, you get the same run-time error.

As pointers are meant to be assigned to memory locations (and point to values), with respect to this basic understanding and the fact that ASCII characters have memory addresses, aren't both code segments technically correct ??

int *ptr=5;

Pointer can only be initialized to zero or null if it does not point to a variable of that type.

char *str="helloworld\n";
printf("string value is %s\n",*str)

The "%s" conversion specification takes a pointer argument not the actual character that *str points to. So if you want to print the entire string...

printf("string value is %s\n", str);

and if you want to print the character that *str points to...

printf("str points to %c\n", *str);

So this is always the case (except for user-defined strings) in real world programming, even though individual ASCII characters (such as 5) have memory addresses ?

Yes ASCII characters have memory addresses though 5 is an integer not an ASCII character. To be intrepreted a character in C it needs to be in single quotes.

char v = '5';

Shamrock, another question do individual integers, f. point numbers and doubles have mem. addressess as well ?

thanks for your help

Yep they do