473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

about pointer initialization

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
4 2809

"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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
111
by: JKop | last post by:
Okay here we go, I feel it's about time people conversed about the bullshit aspects of C++ (including the bullshit stuff brought forward from C). I'll begin with a few of my own grievances: 1)...
3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
3
by: Jeremy Beasley | last post by:
I'm having a problem using pointers to structures. One method of declaring point and then referencing a structure works, while the other generates an error at compile time. -----------------...
21
by: Roman Werpachowski | last post by:
I can't understand this: double * x = new double; const double * p = x; delete p; works. Why am I allowed to delete a const pointer? On the same note: why am I allowed to do this: class...
17
by: prakashraovaddina | last post by:
Hi ppl, can some one say if the following pointer initialization is legal or no. int *intPtr = 5; While debugging i still see that the pointer is given some address. However, the...
21
by: Chad | last post by:
At the following url http://c-faq.com/lib/qsort2.html, they have the following Q: Now I'm trying to sort an array of structures with qsort. My comparison function takes pointers to structures,...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.