naseeam
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 ?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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'.
>> "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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent detailed explanation.
Is not necessarily a pointer to a constant string.
For example, consider the following assignment statement:
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.