-
String literal
If you take the following function call:
Value = GetVal("Please enter a value in the range 1 to 5: ");
then the definition:
int GetVal(const char *const Msg)
When you are passing a string literal as shown in the function call, does the preprocessor allocate memory for this string so the local pointer of the function will hold the address of the string of the first element of the string?
Thanks.
-
Donīt really sure about the memory thing but I do know that the compiler allocates memory for yours Msg char-pointer. That pointer will point at the first character element.
Well lets look at a similair function
int doSomeThing(int number)
the argument that is send to doSomeThing doesnīt allocate new memory(Of course has it to be stores somewhere in the computer internally), but the parameter does. I would guess that the same principle would apply to a const char * const argument.
My guess is that all rvalue doesnīt allocate memory but lvalues does.
-
The local pointer will hold the address of the first element of the string regardless.
The difference that you may be looking at is between char[] and a char* variable.
char[] is, by definition, constant and, therefore, cannot be changed. A char* variable is mutable, i.e. can be incremented, for example.
In this particular case, the char* variable is 'const' and is rendered the same as char[].
As for the preprocessor, I don't know that it has anything to do with the allocation of memory here at all, but someone else may be able to elaborate on this point.
-Skipper
-
Well it would seem logical to me that the string "Please enter a value in the range 1 to 5: " is held in memory, else the char pointer would not be able to evaluate to the address of the string. The question is, does the preprocessor allocate this memory, or some other process?
-
The preprocessor is invoked with the # symbol, i.e. #include, #define.
Since your string literal is passed as an argument from main() - supposedly - I would have to assume that memory allocation is a compiler directive and, therefore, memory is allocated by a process other than through the preprocessor.
(I can negate one (the preprocessor), but can't give you anything more specific on the other, i.e. what the other process is.)
-Skipper
-
Ah, thanks for the reply. I knew something must of been allocating the string memory, ta.