LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2004, 06:40 PM   #1
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
How to declare constant strings in C/C++?


I have a function that returns an error string. The value is actually the address of an internal data member (I don't want to allocate memory and copy the string - I don't trust that the "user" would free it), and consequently, I don't want anyone to screw around with its contents. So how do I declare the return value so that the contents of the string are constant as opposed to the pointer's value being constant? (I hope that made sense)

const char *foo()

(const char) *foo()

<edit>
typedef const char c_char
typedef c_char * c_string

That seems a bit klunky though...
</edit>

I hate having to ask (it seems such a fundamental thing) but I rarely use constant strings other than literals in my code.

Last edited by Dark_Helmet; 05-28-2004 at 06:45 PM.
 
Old 05-28-2004, 06:57 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Original Poster
Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ugh... I should look more closely at my reference material.

From the "C/C++ Programmer's Reference (Second Edition)"
Quote:
Objects of type const cannot be changed by your program during execution. Also, an object pointed to by a const pointer cannot be modified.
As usual, I was trying to make things too complicated.
 
Old 05-28-2004, 08:39 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
NOTE: That only applies to C++ compilers. C++ is much more strict with the const qualifier than C is.
 
Old 05-29-2004, 08:07 AM   #4
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
String literals in C or C++ are constant regardless of whether you assign them to a const variable or not. For example:

char *foo = "some error string";

Results in foo pointing to a constant string (the results are undefined if you try to modify the string; if you're using gcc, the program will crash). So as long as you return a string constant, the user of the function could not modify the error string without crashing their program.

Alex
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
declaration does not declare anything tristanm Programming 5 10-24-2005 04:00 PM
How to declare a defaul argument in my function? bigapple Programming 3 08-11-2005 10:27 PM
g++ compiler error declaration does not declare anything Basiltp Programming 4 10-11-2004 11:29 AM
Help for perl--- Getopt::Declare arobinson74 Programming 1 08-20-2004 10:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration