473,545 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what's the difference between the next 2 declarations?

Hi,

What's the difference between:

char *x="abcde";

and

char y[]={"abcde"};
I can't modify the value of the variable x in functions, but y can be do.

Is there any other similar problem need to be notice?

Thanks in advance!

Wade Yin
Nov 14 '05 #1
48 2619
On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <li********@163 .com>
wrote in comp.lang.c:
Hi,

What's the difference between:

char *x="abcde";
x is a pointer to the a six character (including the '\0') string
literal. Attempting to modify a string literal causes undefined
behavior. x itself can be modified to point to a different array of
characters.
and

char y[]={"abcde"};
y is an array of 6 characters, initialized with the string literal.
The characters in y can be changed.
I can't modify the value of the variable x in functions, but y can be do.

Is there any other similar problem need to be notice?
No, the situation with string literals is a special case and there are
no others like it, except for wide string literals.
Thanks in advance!

Wade Yin


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <li********@163 .com>
wrote in comp.lang.c:
Hi,

What's the difference between:

char *x="abcde";
x is a pointer to the a six character (including the '\0') string
literal. Attempting to modify a string literal causes undefined
behavior. x itself can be modified to point to a different array of
characters.
and

char y[]={"abcde"};
y is an array of 6 characters, initialized with the string literal.
The characters in y can be changed.
I can't modify the value of the variable x in functions, but y can be do.

Is there any other similar problem need to be notice?
No, the situation with string literals is a special case and there are
no others like it, except for wide string literals.
Thanks in advance!

Wade Yin


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3

"Jack Klein" <ja*******@spam cop.net>
??????:1l****** *************** ***********@4ax .com...
On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <li********@163 .com>
wrote in comp.lang.c:
Hi,

What's the difference between:

char *x="abcde";
x is a pointer to the a six character (including the '\0') string
literal. Attempting to modify a string literal causes undefined
behavior. x itself can be modified to point to a different array of
characters.


How does "abcde" stored in memory? we can find x in memory, but we can't
operate it?

and

char y[]={"abcde"};


y is an array of 6 characters, initialized with the string literal.
The characters in y can be changed.
I can't modify the value of the variable x in functions, but y can be do.
Is there any other similar problem need to be notice?


No, the situation with string literals is a special case and there are
no others like it, except for wide string literals.
Thanks in advance!

Wade Yin


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #4

"Jack Klein" <ja*******@spam cop.net>
??????:1l****** *************** ***********@4ax .com...
On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <li********@163 .com>
wrote in comp.lang.c:
Hi,

What's the difference between:

char *x="abcde";
x is a pointer to the a six character (including the '\0') string
literal. Attempting to modify a string literal causes undefined
behavior. x itself can be modified to point to a different array of
characters.


How does "abcde" stored in memory? we can find x in memory, but we can't
operate it?

and

char y[]={"abcde"};


y is an array of 6 characters, initialized with the string literal.
The characters in y can be changed.
I can't modify the value of the variable x in functions, but y can be do.
Is there any other similar problem need to be notice?


No, the situation with string literals is a special case and there are
no others like it, except for wide string literals.
Thanks in advance!

Wade Yin


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #5
Wade Yin wrote:
char *x="abcde";


x is a pointer to the a six character (including the '\0') string
literal. Attempting to modify a string literal causes undefined
behavior. x itself can be modified to point to a different array of
characters.

How does "abcde" stored in memory? we can find x in memory, but we can't
operate it?


Can't "change it", yes, the standard says that you cannot change a
string literal. The implementation has the freedom to place it in a
write-only protected memory segment, in ROM, or in fact anywhere it
desires. It may even put it exactly where it puts a modifiable character
array.

--
John Tsiombikas (Nuclear / the Lab)
nu*****@siggrap h.org
http://thelab.demoscene.gr/nuclear/
Nov 14 '05 #6
Wade Yin wrote:
char *x="abcde";


x is a pointer to the a six character (including the '\0') string
literal. Attempting to modify a string literal causes undefined
behavior. x itself can be modified to point to a different array of
characters.

How does "abcde" stored in memory? we can find x in memory, but we can't
operate it?


Can't "change it", yes, the standard says that you cannot change a
string literal. The implementation has the freedom to place it in a
write-only protected memory segment, in ROM, or in fact anywhere it
desires. It may even put it exactly where it puts a modifiable character
array.

--
John Tsiombikas (Nuclear / the Lab)
nu*****@siggrap h.org
http://thelab.demoscene.gr/nuclear/
Nov 14 '05 #7
Wade Yin <li********@163 .com> spoke thus:
How does "abcde" stored in memory?
Exactly how is (I believe) up to the implementation, but commonly it
ends up in read-only memory.
we can find x in memory, but we can't operate it?


Not without bad things potentially happening - depending on the
implementation, attempting to access memory denoted as read-only can
cause your program to crash.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8
Wade Yin <li********@163 .com> spoke thus:
How does "abcde" stored in memory?
Exactly how is (I believe) up to the implementation, but commonly it
ends up in read-only memory.
we can find x in memory, but we can't operate it?


Not without bad things potentially happening - depending on the
implementation, attempting to access memory denoted as read-only can
cause your program to crash.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9
On Mon, 5 Apr 2004 13:36:27 +0800, "Wade Yin" <li********@163 .com> wrote:

"Jack Klein" <ja*******@spam cop.net>
??????:1l***** *************** ************@4a x.com...
On Mon, 5 Apr 2004 13:05:38 +0800, "Wade Yin" <li********@163 .com>
wrote in comp.lang.c:
> Hi,
>
> What's the difference between:
>
> char *x="abcde";


x is a pointer to the a six character (including the '\0') string
literal. Attempting to modify a string literal causes undefined
behavior. x itself can be modified to point to a different array of
characters.


How does "abcde" stored in memory? we can find x in memory, but we can't
operate it?

If you go to this page:
http://www.bdsoft.com/courses/cprog-details.html
and click on the "slides" link, slides #33-42 illustrate the different ways
in which your two constructs are stored in memory, and the sorts of things
you're permitted to do with them.

(Note to rest of group: I haven't updated this material since I began
hanging out in clc, so there are probably lots of nits to pick WRT saying
what's const and what's not, etc. Rest assured I'll go over the entire
/course/ before I ever actually teach it again....)
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #10

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

Similar topics

8
1450
by: Guybrush Threepwood | last post by:
Hi, I was always wondering if there is any difference between void someFunction(int,int) and void someFunction(int A, int B) is there?
9
5898
by: Vipul Jain | last post by:
Can any one please tell me what is the difference between global scope of an variable and file scope of an variable. Vipul
30
409
by: Wade Yin | last post by:
Hi, What's the difference between: char *x="abcde"; and char y={"abcde"};
18
2231
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
0
1502
suzee_q00
by: suzee_q00 | last post by:
I will go ahead and give you a little background for what I am working on, though I have been hazy on pointers for some time. I get that a pointer points to an address in memory. Today I just learned what dereferencing means (I don't think it sounds like what it does). I am working with linked lists which are pointer intensive. I have a list...
132
4524
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C community. It would seem that C99 is the most up-to-date Standard, but far more people seem to be working off the C89 Standard. Could someone...
9
3169
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will load with different data (locations, departments, etc.).
10
3417
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
0
7502
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7692
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7791
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

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.