473,473 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

constant string

39 New Member
Hi.I have some confusion regarding a constant string and constant pointer to a string.

char *str="hello";
Here I think "hello" is constant so we cant modify it.Then what about
const char *str="hello" and
char *const str="hello"?

Please explain it with examples.thanks.

Jerico
Nov 7 '06 #1
3 3370
horace1
1,510 Recognized Expert Top Contributor
have a look at
http://duramecho.com/ComputerInformation/WhyHowCppConst.html
Nov 7 '06 #2
jerico
39 New Member
I would like to learn it from you.In my previous post,you explained it very thoroughly.Please.

Jerico
Nov 7 '06 #3
horace1
1,510 Recognized Expert Top Contributor
hope this helps!
Expand|Select|Wrap|Line Numbers
  1. // simple examples of const - invalid statements are commented out
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6.     char *s = "hello S";                     // "hello S" is a constant, s is pointed at it   
  7.     char a[30] = "hello A";                  // define an array, initialise it with characters "hello A"
  8.     printf("s = %s a = %s\n", s, a);
  9.     //*(s+2) = 'X';                          // error! change third element of char* pointed to by s 
  10.                                              //   would give run time segmentation error
  11.     a[2] = 'X';                              // change third element of array a 
  12.     printf("s = %s a = %s\n", s, a);
  13.     s=a;                                     // point s at array a
  14.     *(s+2) = 'Z';                            // change third element of char* pointed to by , i.e. a[2]
  15.     printf("s = %s a = %s\n", s, a);
  16.  
  17.     // define pointer s1 to a constant char*, we cannot change the char* s1 points at
  18.     const char *s1 = "hello";                // pointer to a constant  char* 
  19.     //*s1='X';                               // compile time error! - assignment of read-only location
  20.     s1=a;                                    // but we can point s1 at array a
  21.     //*s1='X';                               // compile time error! - although s1 points at a (which can be written)
  22.                                              //    compiler still gives assignment of read-only location
  23.  
  24.     // define pointer s2 a constant pointer to char*
  25.     char *const s2 = a;                      // constant pointer to char *, initialised to point to a
  26.     *s2='Q';                                 // change a[0]
  27.     printf("s2 = %s a = %s\n", s2, a);
  28.     //s2=a;                                  // compile time error! - assignment of read-only location
  29.                                              //  cannot change what s2 points at
  30.  
  31.     // define pointer s3 to a constant pointer to constant char*
  32.     const char *const s3 = a;                // constant pointer to const char *, initialised to point to a
  33.     printf("s3 = %s a = %s\n", s3, a);
  34.  
  35. getchar();
  36. }
  37.  
  38.  
try compiling and running it. then one by one uncomment some of the lines which should cause errors and see what you get.
Nov 8 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Lasse Skyum | last post by:
I have my own string class that I'm generally verry fond of compared to std::string. For that same reason I'm trying to improve a little on it. Since manny objects in my project will contain...
2
by: JJ Feminella | last post by:
This statement is legal C#: public const string day = "Sunday"; but this statement is not: public const string days = new string { "Sun", "Mon", "Tue" }; The C# Programmer's Reference...
4
by: Jon Shemitz | last post by:
How come const string S = "string"; // valid is valid but const Type T = typeof(string); // invalid is not? "C# Language Specification" chapter 7.15 defines a constant expression as...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
3
by: lovecreatesbeauty | last post by:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the "hello, world" thing using the name "string-constant". But `ISO/IEC 9899:TC2' does not include this kind of thing in section `A.1.5...
25
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with...
20
by: karthikbalaguru | last post by:
Hi, String constant being modifiable in C++ but, not modifiable in C. that is, In C++, the following example output will be "Mplusplus" char *str1 = "Cplusplus"; *str1 = 'M'; In C, the above...
18
by: sinbad | last post by:
hi, why does the following program gives an runtime error ,instead of compilation error. anyone please shed some light. thanks sinbad ------------------------------ int main()
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
8
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.