By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

constant string

P: 39
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
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
have a look at
http://duramecho.com/ComputerInformation/WhyHowCppConst.html
Nov 7 '06 #2

P: 39
I would like to learn it from you.In my previous post,you explained it very thoroughly.Please.

Jerico
Nov 7 '06 #3

Expert 100+
P: 1,510
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

Post your reply

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