473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Life time of literal string

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
Sep 8 '08 #1
4 2169
On 8 Sep, 14:53, Nagrik <vnag...@gmail.comwrote:
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")
--
Nick Keighley

Sep 8 '08 #2
On Sep 8, 4:53 pm, Nagrik <vnag...@gmail.comwrote:
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.
Sep 8 '08 #3
Nagrik wrote:
>
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 8 '08 #4
CBFalconer <cb********@yahoo.comwrites:
Nagrik wrote:
>>
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.
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'".
>char* retstr() { return "literalstring";}
--
Ben.
Sep 8 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Ney André de Mello Zunino | last post by:
Hello. The following is a summary of what I have understood from reading Stroustrup's TCPL 3rd Edition's discourse on "string literals" (5.2.2). String literals are of type const char, where n...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
1
by: Johan Lindh | last post by:
This is somewhat obscure, and I'm not sure if the question is valid in comp.lang.c. If it isn't, please ignore it. In a C program, the pointer resulting from a __FILE__ is stored. The pointer...
5
by: sandman | last post by:
I've been testing my web app on another workstation to simulate using the server time. The test pc's time is an hour behind the server time and when the user processes a request, the server time...
0
by: bsm | last post by:
Hi, I created GridView and created "BoundColumn" at runtime. When I try to create TemplateColumn it throws some error. But the same is working in Design Time Setting. Design code (Working...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.