printf("%s", *p) is wrong. It should be either printf("%s", p) to print the whole string, or printf("%c", *p) to print the first character.
–
interjayMay 16 '13 at 17:25

1

This is really a complex detail, and you need to get it right. You need to study some textbooks before you go much farther. But anything * in a declaration means "pointer to an anything entity". So char *means "pointer to a single character". By convention, since C does not have a "string" type, the meaning of char * is overloaded to also sometimes mean "pointer to the first character in a sequence of related characters, probably terminated by a zero byte". But this is only a convention, it is not a part of the language.
–
Hot LicksMay 16 '13 at 17:30

"char means to read ..." - Huh? That's not right. Either your explanation or your understanding is a few steps off. A char is and means nothing else, other than being a char. And a char* is nothing more than a memory address of a char. There is no "destination" except maybe the null-terminator at the end of a 'string', but char knows nothing about this. But printf will loop through and find the null-terminator for %s.
–
DukelingMay 16 '13 at 17:32

well I'm not really concerned about the syntex errors I wrote, I just typed mindlessly while concentrating on my point and made a mistake. I know printf refers to an address type. well anyways that wasn't my point either, well still thanks for the answer, and about the second question I'm not sure if your 100% right. because according to the declaration, assuming that pointer have data types, int* a,b; it declares int *a & int b seperately which I thinkthe compiler doesn't consider the pointer as a data type
–
ksp0422May 16 '13 at 17:38

5 Answers
5

The pointer p does, in fact, only point to the first byte. In this case, byte 0. But if you use it in the context of

printf("%s", p)

Then printf knows to print until it gets the null character \0, which is why it will print the entire string and not just 'h'.

As for the second question, pointers do posses a data type. If you were to say it outloud, the name would probably be something like type "pointer to a character" in the case of p, or "pointer to an integer" for int *i.

Pointer variables don't hold a data type, they hold an address. But you use a data type so you know how many bytes you'll advance on each step, when reading from memory using that pointer.

When you call printf, the %s in the expression is telling the function to start reading at the address indicated by *p (which does hold the byte value for 'h', as you said), and stop reading when it reaches a terminating character. That's a character that has no visual representation (you refer to it as \0 in code). It tells the program where a string ends.

Here *p is a pointer to some location in memory, that it assumes to be 1 byte (or char). So it points to the 'h' letter. So p[0] or *(p+0) will give you p. But, your string ends with invisible \0 character, so when you use printf function it outputs all symbols, starting from the one, where *p points to and till `\0'.

And pointer is just a variable, that is able to hold some address (4, 8 or more bytes).

Pointer is derived data type, each data type can have a pointer associated with. Pointers don't have a keyword, but are marked by a preceding * in the variable and function declaration/definition. Most compilers supplies the predefined constant NULL, which is equivalent to 0.