what's the difference between the next 2 declarations?

W

Wade Yin

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
 
J

Jack Klein

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.
 
W

Wade Yin

Jack Klein said:
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
 
J

John Tsiombikas (Nuclear / the Lab)

Wade said:
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.
 
C

Christopher Benson-Manica

Wade Yin said:
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.
 
L

Leor Zolman

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
 
D

Dan Pop

In said:
Exactly how is (I believe) up to the implementation, but commonly it
ends up in read-only memory.

It's less common than you might think. Read-only string literals are a
C89 invention and many implementors have chosen compatibility with
pre-ANSI C programs. In pre-ANSI C, string literals were writable and
the compiler was not free to merge multiple identical string literals.
Some compilers (e.g. gcc) even let the user choose between the two
flavours of string literals:

-fwritable-strings
Store string constants in the writable data segment and don't
uniquize them. This is for compatibility with old programs
which assume they can write into string constants.
Not without bad things potentially happening - depending on the
implementation, attempting to access memory denoted as read-only can
cause your program to crash.

Well I can't find anything wrong with read-only accesses to read-only
memory...

Dan
 
J

Jack Klein

It's less common than you might think.

....in desk top and workstation type implementations. It is very
common in embedded implementation, so much so in fact that I would
seriously consider rejecting a compiler that copied string literals
from read-only storage to RAM by default.
 
D

Dan Pop

In said:
...in desk top and workstation type implementations.

I.e. in what the standard calls hosted implementations, which happen to
be the default implementations for this newsgroup.

Dan
 
E

E. Robert Tisdale

Dan said:
Wade said:
What's the difference between:

char *x="abcde";

and

char y[]={"abcde"};

How about reading the FAQ *before* posting?

Why would Wade read the [C] FAQ *before* posting?
Does the FAQ answer this question?
Can you cite and quote the relevant FAQ?
 
R

Richard Bos

E. Robert Tisdale said:
Dan said:
How about reading the FAQ *before* posting?

Why would Wade read the [C] FAQ *before* posting?

Because it is nothing but polite to read the FAQ for _any_ newsgroup you
intend to start posting to. So is reading a reasonable amount of
backlog. It's basic netiquette, really. Which explains why it's done so
rarely :-(

Richard
 
A

anonymous

The FAQ shows the declaration like:

char a[] = "string literal";

But in the OP's question it is like:

char y[] = {"string literal"};

Are they same?
 
D

Dan Pop

In said:
Dan said:
Wade said:
What's the difference between:

char *x="abcde";

and

char y[]={"abcde"};

How about reading the FAQ *before* posting?

Why would Wade read the [C] FAQ *before* posting?
Does the FAQ answer this question?

Yup. In "graphic" detail.
Can you cite and quote the relevant FAQ?

Yup, but only if you openly admit that you cannot find it yourself.

Dan
 
D

Dan Pop

In said:
Sure, most of things can be deal with by FAQ,

But It's too long to fit for a single question, Right?!

Wrong! The FAQ is divided into sections so that you don't have to
read the parts that are obviously irrelevant to your question. And your
question is the second in the section... Arrays and Pointers, i.e. the
very place where an intelligent person would have searched the answer.

So, you have NO excuse for not doing your homework before posting...

Dan
 
D

Dan Pop

In said:
The FAQ shows the declaration like:

char a[] = "string literal";

But in the OP's question it is like:

char y[] = {"string literal"};

Are they same?

Why would they be any different? What difference do you expect those
superfluous braces to make?

FYI:

int i = {1};

is also allowed. I hope you're not going to ask me if it's the same as

int i = 1;

;-)

Dan
 
C

CBFalconer

Richard said:
E. Robert Tisdale said:
Dan said:
How about reading the FAQ *before* posting?

Why would Wade read the [C] FAQ *before* posting?

Because it is nothing but polite to read the FAQ for _any_
newsgroup you intend to start posting to. So is reading a
reasonable amount of backlog. It's basic netiquette, really.
Which explains why it's done so rarely :-(


+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT | :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
==============================================================

Trollsdale only does it to annoy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top