hello
please can someone tell me
is it ok to write
char *s = "hello";
or to write
char *s;
s = "hello";
i want to know what is the difference
what about
int *i = 7; is it acceptable? and what will it do?
Thanks
hello
please can someone tell me
is it ok to write
char *s = "hello";
or to write
char *s;
s = "hello";
i want to know what is the difference
what about
int *i = 7; is it acceptable? and what will it do?
Thanks
please set free our POWs
char* s = "hello"; // is acceptable
char* s;
s = "hello world"; // Not allowed.
int* i = 7; // assigns i to point to address 7; not acceptable or clear code
While you're breakin' down my back n'
I been rackin' out my brain
It don't matter how we make it
'Cause it always ends the same
You can push it for more mileage
But your flaps r' wearin' thin
And I could sleep on it 'til mornin'
But this nightmare never ends
Don't forget to call my lawyers
With ridiculous demands
An you can take the pity so far
But it's more than I can stand
'Cause this couchtrip's gettin' older
Tell me how long has it been
'Cause 5 years is forever
An you haven't grown up yet
-- You Could Be Mine - Guns N' Roses
hi
thank you for your response
you said that
char* s;
s = "hello world"; // Not allowed
i've compiled it and there were no errors
so why it's not allowed? what will it do?
please set free our POWs
It is not a part of the standard. It is assigning a const array of chars to a pointer to char variable.
While you're breakin' down my back n'
I been rackin' out my brain
It don't matter how we make it
'Cause it always ends the same
You can push it for more mileage
But your flaps r' wearin' thin
And I could sleep on it 'til mornin'
But this nightmare never ends
Don't forget to call my lawyers
With ridiculous demands
An you can take the pity so far
But it's more than I can stand
'Cause this couchtrip's gettin' older
Tell me how long has it been
'Cause 5 years is forever
An you haven't grown up yet
-- You Could Be Mine - Guns N' Roses
It actually *is* legal, and it works quite nicely, but however, there should be a change to BOTH pieces of code:
const char *s = "hello";
const char *s;
s = "hello";
The second is fully legal; the type of a string literal is "const char *" so it is fully legal. Without the const, it still works, but this is a bad thing to use -- this is the only instance in C++ of an automatic cast from a const to a non-const, and, like ANY cast from const, it can easily be abused.
In fact, the cast is listed as deprecated, so new code should never use it; it may be phased out eventually.
This, however, is NOT legal:
char arr[6];
arr = "hello";
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
Technically, the former is an example of initialisation whereas the latter is an example of assignment. Other than that, there is no significant difference in the example as posted.Originally posted by kuwait
hello
please can someone tell me
is it ok to write
char *s = "hello";
or to write
char *s;
s = "hello";
i want to know what is the difference
If you know, when you declare the pointer, that you wish to initialise it with a value straight away then it makes sense to use the former (initialisation) rather then to declare it and follow with an assignment straight afterwards, but it's no big deal.