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

about pointer initialization

P: n/a
ex1.char q[]="Nick";
q[0]='D';

ex2.char *q ="Nick";
q[0]='D';

ex1 is correct, but ex2 occurs run time error.

why?

thanks!
Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"nick" <i1********@yahoo.com> wrote in message
news:dl***********@justice.itsc.cuhk.edu.hk...
ex1.char q[]="Nick";
q[0]='D';

ex2.char *q ="Nick";
q[0]='D';

ex1 is correct, but ex2 occurs run time error.

why?


Because attempting to modify a string literal
produces undefined behavior, and 'run time error'
is one of an infinite number of possible manifestations
of undefined behavior. Moral: Don't Do That.

-Mike
Nov 22 '05 #2

P: n/a
I'm surprised this isn't in the FAQ... so here goes:

nick <i1********@yahoo.com> wrote in news:dl6o61$1j4k$1
@justice.itsc.cuhk.edu.hk:
ex1.char q[]="Nick";
q is an array of 5 bytes, into which is copied 'N' 'i' 'c' 'k' '\0'
q[0]='D';
Reassigns the first byte.

ex2.char *q ="Nick";
q is a pointer to the string literal "Nick", stored somewhere in memory,
potentially in a read-only memory page. A more "correct" way to write this
line is:

const char * q = "Nick";

C++ accepts the non-const version in order to be more compatable with C.
q[0]='D';


Attempts to change the first byte, which is const. As a result, Undefined
Behaviour (in your case, a run-time error).

Nov 22 '05 #3

P: n/a
In article <dl***********@justice.itsc.cuhk.edu.hk>,
nick <i1********@yahoo.com> wrote:
ex1.char q[]="Nick";
q[0]='D';

ex2.char *q ="Nick";
q[0]='D';

ex1 is correct, but ex2 occurs run time error.

why?


See http://www.comeaucomputing.com/techtalk/#stringliteral
http://www.comeaucomputing.com/techtalk/#ptrvsarray
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #4

P: n/a

"nick" <i1********@yahoo.com> wrote in message
news:dl***********@justice.itsc.cuhk.edu.hk...
| ex1.char q[]="Nick";
| q[0]='D';
|
| ex2.char *q ="Nick";
| q[0]='D';
|
| ex1 is correct, but ex2 occurs run time error.
|
| why?
|
| thanks!

ex1.char is not a type.
ex2.char is not a type.
Who knows, maybe ex1 and ex2 are actually types?
So which would q be, the pointer or the array?

Lets suppose...

char q[] = "Nick";
char *p = &q[0]; // its a single char
*p = 'S';
std::cout << q; // prints "Sick" (note*)

Note: lets not get confused here, a char array is not handled like
arrays of other primitive types or arrays of user-defined types when it
comes to output. You can thank compatibility for that headache. A
std::string is a much better candidate here.

std::string s("I'm a string");
std::string *p_s = &s; // pointer to an entire string, not just a char
<ah, relief>

Are ex1 and ex2 structs or classes? That would be nice to know. If
so...char is reserved.

struct ex1 {
char c;
};

ex1 q[] = {'a', 'b', 'c'}; // treat them as what they are
ex1 *p = &q[0]; // p points to the first element

ex1 *p2 = {'u'}; // error
at which point p2 is undefined, null, zipfa, dangerous, bad karma, UB
(pick one)

An unitialized pointer is nothing, in my humble opinion: its not even a
pointer. Call it a null pointer if you like. To me its a black hole. A
few will argue, and disscuss, and scream, and beg but its still a black
hole here. Adamantly so.

The closer you get to a black hole, and the more often you get anywhere
near it, it's eventually bound to suck you in.


Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.