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
3 3370 horace1 1,510
Recognized Expert Top Contributor
have a look at
http://duramecho.com/ComputerInformation/WhyHowCppConst.html
I would like to learn it from you.In my previous post,you explained it very thoroughly.Please.
Jerico
horace1 1,510
Recognized Expert Top Contributor
hope this helps! -
// simple examples of const - invalid statements are commented out
-
#include <stdio.h>
-
-
int main()
-
{
-
char *s = "hello S"; // "hello S" is a constant, s is pointed at it
-
char a[30] = "hello A"; // define an array, initialise it with characters "hello A"
-
printf("s = %s a = %s\n", s, a);
-
//*(s+2) = 'X'; // error! change third element of char* pointed to by s
-
// would give run time segmentation error
-
a[2] = 'X'; // change third element of array a
-
printf("s = %s a = %s\n", s, a);
-
s=a; // point s at array a
-
*(s+2) = 'Z'; // change third element of char* pointed to by , i.e. a[2]
-
printf("s = %s a = %s\n", s, a);
-
-
// define pointer s1 to a constant char*, we cannot change the char* s1 points at
-
const char *s1 = "hello"; // pointer to a constant char*
-
//*s1='X'; // compile time error! - assignment of read-only location
-
s1=a; // but we can point s1 at array a
-
//*s1='X'; // compile time error! - although s1 points at a (which can be written)
-
// compiler still gives assignment of read-only location
-
-
// define pointer s2 a constant pointer to char*
-
char *const s2 = a; // constant pointer to char *, initialised to point to a
-
*s2='Q'; // change a[0]
-
printf("s2 = %s a = %s\n", s2, a);
-
//s2=a; // compile time error! - assignment of read-only location
-
// cannot change what s2 points at
-
-
// define pointer s3 to a constant pointer to constant char*
-
const char *const s3 = a; // constant pointer to const char *, initialised to point to a
-
printf("s3 = %s a = %s\n", s3, a);
-
-
getchar();
-
}
-
-
try compiling and running it. then one by one uncomment some of the lines which should cause errors and see what you get.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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()
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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: 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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |