why this is wrong??

V

Virtual_X

char *x[10] = {"abc","eqx",.....};
this is valid

and the same in int in not valid

int *x[10] = {1,2,3,4,....};
--------------------------------------------------------------
and also

char *seg= "CPP";

seg[1]= 'x'; // or *seg= 'x';

cout << seg << endl;

why the program crash when running the above code and not to print
CXP
 
A

Andre Kostur

char *x[10] = {"abc","eqx",.....};
this is valid

Sure.. array of 10 char* to the specified string literals.
and the same in int in not valid

int *x[10] = {1,2,3,4,....};

Array of 10 int*, and they're being initialized with ints. No good. A
pointer cannot be initialized with an int (without a suitable hammer, er,
cast).

Pointer to a string literal.
seg[1]= 'x'; // or *seg= 'x';

Undefined behaviour. Modifying a string literal.
cout << seg << endl;

why the program crash when running the above code and not to print
CXP

Because you attempted to modify a string literal. What would work for
you:

char seg[] = "CPP"; // This is an array being initialzed from the
string literal

seg[1] = 'x';

cout << seg << endl;
 
J

Jim Langston

Virtual_X said:
char *x[10] = {"abc","eqx",.....};
this is valid

Yes. The result of "abc" is a [const] char * pointing to the first
character in the string.
and the same in int in not valid

int *x[10] = {1,2,3,4,....};

Correct, the result of 1 is the number 1, not a pointer. It should be
changed to:
int x[10] = { 1,2,3,4,...};

seg points to the constant string "CPP" which should not be changed
(undefined behavior).
seg[1]= 'x'; // or *seg= 'x';

You are now trying to change constant data, undefined behavior.
 
J

James Kanze

char *x[10] = {"abc","eqx",.....};
this is valid

But deprecated, and bad practice. Correctly, it should be:

char const* x[10] = { "abc" ... } ;
and the same in int in not valid

The same with int isn't even possible, since the language
doesn't have literals of type int const[].
int *x[10] = {1,2,3,4,....};

That's not the same at all. In the first, you use string
literals as initializers. A string literal has type char
const[N], where N is one more than the length of the string; an
array converts implicitly to a pointer in this context, so you
are initializing an array of char const* with char const*. In
the second case, you use integer literals as initializers. And
integer literal has type int (unless it is too large to fit, or
has a suffix), and int doesn't convert implicitly to int*.
char *seg= "CPP";
seg[1]= 'x'; // or *seg= 'x';
cout << seg << endl;
why the program crash when running the above code and not to print
CXP

Because the type of "CPP" is char const[4], and you're not
allowed to modify a const object.
 

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,056
Latest member
GlycogenSupporthealth

Latest Threads

Top