difference between between these "char"s

A

arnuld

what is the difference between these 2:

char name = "hackers";
char* name = "hackers";
 
A

arnuld

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 ?
 
S

Sarath

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 ?

I dont this think that
char name = "hackers"; statement is a valid one.

char name means a variable of type char which has one byte in size.

Regards,
Sarath
 
J

Jerry Coffin

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

?
 
A

arnuld

The main difference is that the second will compile, but the first one
won't.

that i did not know. may i know why ?
Perhaps you intended:

char name[] = "hackers";
vs.
char *name = "hackers";

?

what's the difference between these 2 ?
 
I

Ian Collins

arnuld said:
that i did not know. may i know why ?

A char can only be initialised with a character constant, not a string
literal. A string literal is a const char*.
Perhaps you intended:

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*
 
A

arnuld

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 ?
 
I

Ian Collins

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

arnuld

arnuld said:
in both cases we will get a string, so it doe snot matter we use which
one.

Very very wrong!

char name[] = "hackers";

Is an automatic array of char, as such, it can be modified.

ok, i new that.
char *name = "hackers";

Is the incorrect way of writing

const char *name = "hackers";

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.

A string literal should not be modified, to do so invokes undefined
behaviour and my cause your toilet to explode.

whwn will my kitchen explode ?

;-)



 
R

Rolf Magnus

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.

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

A string literal is also an array of char.
 
A

arnuld

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.

:-(

i really hate that decision of Stroustrup.
A string literal is also an array of char.

the difference is a "string literal", is a /const/ array of char

?
 
R

Rolf Magnus

arnuld said:
the difference is a "string literal", is a /const/ array of char

?

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.
 
A

arnuld

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.

thanks, that was new thing you told, static allocation of string
literal.

now i can say i know about char*

:)
 
G

Gavin Deane

now i can say i know about char*

Don't forget that C++ gives you std::string to remove the need to
worry about these complications. That's not to say you shouldn't
understand raw char arrays too - you certainly should - but now you've
learnt how the hard way works don't forget you have the easy way
available to you too.

Gavin Deane
 
B

Bo Persson

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 ?

No, not quite right.

Have you read chapter 5 yet? :)


Bo Persson
 
B

Bo Persson

arnuld said:
:-(

i really hate that decision of Stroustrup.

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!
the difference is a "string literal", is a /const/ array of char

?

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.


Bo Persson
 
A

arnuld

arnuld wrote:

It wasn't for him to decide, it is inherited from the C language.

that was what i said. his decision for "keepiing backward
compatibility with C" was a bad decision. we got a complex beast in
the end, named C++.

but please leave it here. i do not want it to become the topic of
discussion. i want the topic to be "chat and char*", so i will not
talk about any OT things. my mistake.

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.

what does last sentence mean:

"a pointer to const to a pointer to non-const"


this pointer to a char notation:

char* x; or
char *x;

how will you define your last sentence in this notation?
Also, even though x is of type char*, in C++ it is still illegal to assign
to *x. The string is a constant!

did not get it. IMVHO, please explain using an example.


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.

i know about "std::strings" and understand how they work. i only want
to understand the "char and char*" for now.

In C++ you hardly ever have to use char* directly.

YES, you use "std::string". i am saying, it is important to understand
"char and char*" when you have understood "std::string".

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.

to me, i do not think pointers are difficult than Templates. i do not
have any teacher/mentor who can teach me, except you folks. all of the
colleges here teach a language named VC++ & they do not agree that
"Linux is a good OS" and i am a UNIX man, that is the trouble.
 
A

arnuld

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]$
------------------------

no undefined behaviour, i modified the "const char*". now, If i can
modify "const", what is the significance of "const" then ? [1]


-- arnuld
http://arnuld.blogspot.com

[1] but a "const int/char/float" can not be modified. i have checked
that.
 
G

Greg Comeau

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:

Ian said a string literal should not be modified, he did not say...
#include <iostream>

int main() {
int x = 7;
const char* author = "Stroustrup";

std::cout << x
<< "\t"
<< author
<<"\n";

x = 100;
author = "Bjarne";

... that you cannot reassign to a named const char *.
author is not a string literal in your example, it is a pointer
to one (actually two of them over your apps lifetime) in your case.
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]$

You didn''t modify const. The const here, and where UB would come
in, is if you did something like:

*author = 'b';

to make his name lowercase.
[1] but a "const int/char/float" can not be modified. i have checked
that.

Nor can const other things. But here, the pointer is not const,
the thing is points to is, or at least should be considered so
through author.

And atop that, a string literal, as Ian correctly points out,
should not be considered a modifiable entity.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top