Life time of literal string

N

Nagrik

Dear Group,

I understand that if the literal string is bound to a variable then it
goes in a special place in the memory and remains with the program
till the end and its characters can not be manipulated. But however
the following example. Will the literal string (returned to variable
retval from function retstr) will remain with the program forever in
the following examples.


#include <stdio.h>

char* retstr();

char* retstr() { return "literalstring";}

int main() {

char* retval = retstr();

return 0;

}


Thanks for your help.

nagrik
 
N

Nick Keighley

I understand that if the literal string is bound to a variable then it
goes in a special place in the memory and remains with the program
till the end and its characters can not be manipulated.

you can miss out the "bound to a variable" bit. A literal string's
lifetime is the same as the lifetime of the program. A literal string
cannot be modified (or rather, it is Undefined Behaviour to modify a
string literal).
 But however
the following example.  Will the literal string (returned to variable
retval from function retstr) will remain with the program forever in
the following examples.

#include <stdio.h>

char* retstr();

char* retstr() { return "literalstring";}

int main() {

char* retval = retstr();

return 0;

}

yes (provided "for ever" = "lifetime of program")
 
V

vippstar

Dear Group,

I understand that if the literal string is bound to a variable then it
goes in a special place in the memory and remains with the program
till the end and its characters can not be manipulated. But however
the following example. Will the literal string (returned to variable
retval from function retstr) will remain with the program forever in
the following examples.

#include <stdio.h>

char* retstr();

char* retstr() { return "literalstring";}

int main() {

char* retval = retstr();

return 0;

}

Yes, that's valid.
 
C

CBFalconer

Nagrik said:
I understand that if the literal string is bound to a variable
then it goes in a special place in the memory and remains with
the program till the end and its characters can not be
manipulated. But however the following example. Will the
literal string (returned to variable retval from function retstr)
will remain with the program forever in the following examples.

#include <stdio.h>
char* retstr();

The above is pointless. The declaration provided a prototype.
However you should specify the function parameter as 'void'
(without the quotes).
char* retstr() { return "literalstring";}

int main() {
char* retval = retstr();
return 0;
}

You should always get the same pointer back from retstr. There is
nothing special about the storage location.
 
B

Ben Bacarisse

CBFalconer said:
The above is pointless. The declaration provided a prototype.

I think you are confused. Did you mean to write "definition provides"
rather then "declaration provided"? Either way, I think you are
wrong. The above is a declaration which is immediately followed by a
definition of the declared function, but neither provides a prototype
for the function.
However you should specify the function parameter as 'void'
(without the quotes).

I would not call this "the function parameter" -- it is clearly
intended to have none. Better to say "specify the function parameter
*list* as 'void'".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top