Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

Constant string is of type char * ?

How is constant string (that is, one in double quotes) is of type char *    ?  

Does this mean constant string is an address?  

For example, "Hello!" is a constant string.  How can it be address ?   When you print this constant string with %s format specifier, "Hello!" will be printed.  Is "Hello!"  an address ?
SOLUTION
Avatar of Dr. Klahn
Dr. Klahn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phoffric
phoffric

>> string (that is, one in double quotes)
 Is not necessarily a pointer to a constant string.
 For example, consider the following assignment statement:
char strg[] = "12345";

Open in new window

This variable strg holds a string, but the string is not a constant string. It can be overwritten.

 In this case the literal string constant "12345" does not exist as its own entity . Since this is an assignment statement, the variable gets a copy of the chars 1, 2, 3, 4, 5 followed by a null byte into its array, so that it's array size is six bytes.
Avatar of naseeam

ASKER

My questions is that Beej's Guide to Programming in C  says constant string is a pointer to char ?  How can it be a pointer?  It's a constant string.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just wanted to clarify one point.
>>   "Hello!" is a constant string.  How can it be address ?

Since the expression, "Hello!", is a type of char*, i.e., a pointer to a char.
A char* pointer contains the address of the char that it is pointing to.
Then you can see that "Hello!" returns a data type that is a char* pointer; and this pointer contains the address of the char 'H'.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of naseeam

ASKER

Excellent detailed explanation.