Originally Posted by
josnow
When you say we can't dereference a char, why not?
I am kind of new to pointers.
Think of pointers as any normal variable, but instead of containing a "value" it contains a memory address.
The declaration:
Declares a variable called 'p' (not '*p') of 'char *' type. So 'p' will store a memory address.
"Dereference" is the operation to use the "reference" (the pointer variable) as a memory address and get data 'pointed' by it.
In a crude way:
Code:
char *p; // declare p as a pointer to a char.
p = 0x6001a3; // Puts the address 0x6001a3 in p.
*p = 'a'; // puts the character 'a' in the address contained in p.
But, of course, instead of initializing 'p' with a literal address you'll get the "address of" another symbols, for example:
Code:
char s[] = "fred";
char *p;
p = &s[1]; // p is assigned with the 'address of' s[1].
putchar( *p ); // will print 'r'