A
arnuld
what is the difference between these 2:
char name = "hackers";
char* name = "hackers";
char name = "hackers";
char* name = "hackers";
char* name = "hackers";
char name = "hackers";
i do not know anything about this.
char* name = "hackers";
i guess, this will become an array of "char"s and name will point to
1st element of the array .i.e.
name and name[0] will mean same
right ?
what is the difference between these 2:
char name = "hackers";
char* name = "hackers";
The main difference is that the second will compile, but the first one
won't.
Perhaps you intended:
char name[] = "hackers";
vs.
char *name = "hackers";
?
arnuld said:that i did not know. may i know why ?
The former initialises an array of char withPerhaps you intended:
char name[] = "hackers";
vs.
char *name = "hackers";
?
what's the difference between these 2 ?
char name[] = "hackers";
vs.
char *name = "hackers";
?what's the difference between these 2 ?
The former initialises an array of char with
{'h','a','c','k','e','r','s','\0'} the latter initialises a char* with
the address of the string literal "hackers" in this case, the compiler
may issue a diagnostic due the the type of a string literal being const
char*
Very very wrong!arnuld said:it means we can there are 2 ways to create a string:
an array and
a char*
in both cases we will get a string, so it doe snot matter we use which
one.
right ?
arnuld said:in both cases we will get a string, so it doe snot matter we use which
one.right ?
Very very wrong!
char name[] = "hackers";
Is an automatic array of char, as such, it can be modified.
char *name = "hackers";
Is the incorrect way of writing
const char *name = "hackers";
A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.
const char*.arnuld said:1.) we can use /char*/ where we are damn sure that we need a constant
string.
arnuld said:that is really strange for me as i did not know. i collect 2 points
here:
1.) we can use /char*/ where we are damn sure that we need a constant
string.
2.) a string is different from string literal. a /string/ is an /
array/ of /char/ and a /string literal/ is /char*/ and that can not be
modified.
Yes, however, this is only true if you directly initialize it with a string
literal. That's a stupid special rule to make C++ more compatible with C
code written by sloppy programmers.
A string literal is also an array of char.
arnuld said:the difference is a "string literal", is a /const/ array of char
?
arnuld wrote:
I'd call it an array of const char, but yes. Also, no matter where it's
defined in the source code, it has static storage duration, i.e. exists
throughout the whole execution time of the program.
now i can say i know about char*
arnuld said:char name[] = "hackers";
vs.
char *name = "hackers";
what's the difference between these 2 ?
The former initialises an array of char with
{'h','a','c','k','e','r','s','\0'} the latter initialises a char*
with the address of the string literal "hackers" in this case, the
compiler may issue a diagnostic due the the type of a string literal
being const char*
it means we can there are 2 ways to create a string:
an array and
a char*
in both cases we will get a string, so it doe snot matter we use which
one.
right ?
arnuld said::-(
i really hate that decision of Stroustrup.
the difference is a "string literal", is a /const/ array of char
?
arnuld wrote:
It wasn't for him to decide, it is inherited from the C language.
At the time when C++ was designed, C didn't have the keyword const, so all
string literals were of the type char*. That made code like
char* x = "Hello";
valid. In C++ string literals have the type 'const char*', but this old C
code has got a special dispensation so that it will continue to work anyway.
Everywhere else it is an error to assign a pointer to const to a pointer to
non-const.
Also, even though x is of type char*, in C++ it is still illegal to assign
to *x. The string is a constant!
At this time it could be a good idea to take a peak at chapter 20 about
std::string, which solves these kinds of problems the C++ way.
In C++ you hardly ever have to use char* directly.
You have now found exactly the reason why many in this group recommended
"Accelerated C++", which introduces std::string on the first page of chapter
1. The subtle difficulties of pointers and arrays are postponed to chapter
10, long after teaching about the simpler things like classes, templates and
overloaded functions.
arnuld wrote:
Very very wrong!
char name[] = "hackers";
Is an automatic array of char, as such, it can be modified.
char *name = "hackers";
Is the incorrect way of writing
const char *name = "hackers";
A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.
On Mar 4, 12:12 pm, Ian Collins <[email protected]> wrote:arnuld wrote:Very very wrong!
char name[] = "hackers";
Is an automatic array of char, as such, it can be modified.
char *name = "hackers";
Is the incorrect way of writing
const char *name = "hackers";
A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.
a "const char*" can easily be modified, see:
#include <iostream>
int main() {
int x = 7;
const char* author = "Stroustrup";
std::cout << x
<< "\t"
<< author
<<"\n";
x = 100;
author = "Bjarne";
std::cout << x
<< "\t"
<< author
<<"\n";
}
---------- OUTPUT
[arch@voodo cpp]$ g++ -ansi -pedantic -Wall -Wextra char-po.cpp
[arch@voodo cpp]$ ./a.out
7 Stroustrup
100 Bjarne
[arch@voodo cpp]$
[1] but a "const int/char/float" can not be modified. i have checked
that.
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.