By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Extremely Simple Question

P: n/a
what's wrong with this, and .. can you not declare "char" variables?

#include <iostream>

int main()
{
int a = "* * * * * * * *\n";
std::cout << a;
return EXIT_SUCCESS;
}

thx
Jul 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Mike Lundell wrote:
what's wrong with this, and .. can you not declare "char" variables?

blah blah blah

oops, sorry for duel post.. my time is off on my box so i didn't see the
post as most recent.
Jul 19 '05 #2

P: n/a
Mike Lundell wrote:
what's wrong with this, and .. can you not declare "char" variables?

#include <iostream>

int main()
{
int a = "* * * * * * * *\n";
std::cout << a;
return EXIT_SUCCESS;
}

thx

You cannot convert a string to an int.

"* * * * * * * *\n" is from type char*,
which means that you can represent this
string as a pointer on the first character.
A function or method, that would handle
with this string, would start from this
first pointer and would go along this
string until it finds the end of the
string, which is marked with 0 (don't
mix it up with "0", which would be the
character "0").

What you can do, is somethind like

int a = 'k';

which convert the 'k' into it's ASCII
number. That's why you can also do
things like 'if ('a' < 'b') std::cout <<
"hello world"'.
This works only with single characters
and not strings.

solution: replace

int a = "* * * * * * * *\n";

with

char* a = "* * * * * * * *\n";

bye Boris

Jul 19 '05 #3

P: n/a


Allan Bruce wrote:

char* a = "* * * * * * * *\n";

bye Boris

This is wrong too - you either need


why?
char a[] = "* * * * * * * *\n";

or

char *a = new char[16] = "* * * * * * * *\n";
That, my friend, is seriously wrong. You can't assign
arrays in C++, not even character arrays.

char *a = strcpy( new char[17], "* * * * * * * *\n" );

would work, but it's not a good idea most of the time.


The only thing that could be made better in

char* a = "* * * * * * * *\n";

is to replace the data type of the pointer to

const char* a = "* * * * * * * *\n";

but other then that, Boris is completely correct.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4

P: n/a
Allan Bruce wrote in news:bg**********@news.freedom2surf.net:

or

char *a = new char[16] = "* * * * * * * *\n";


#include <iostream>
#include <ostream>

int main()
{
char *a = "* * * * * * * *\n";
char *b = new char[16] = "* * * * * * * *\n";
char *c = new char[16];

std::cout << "a: " << ((void *)a) << std::endl;
std::cout << "b: " << ((void *)b) << std::endl;
std::cout << "c: " << ((void *)c) << std::endl;
}

The output I get (MSVC 7.1) is:

a: 0041E01C
b: 0041E030
c: 002F0F88

I was supprised that my compiler even let me write this nonsense.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5

P: n/a
Rob Williscroft wrote:
char[16]


is that like a new instance of the "char" class defined as char *c?

if so, i'm guessing the 16 is for how many characters are defined for that
var. (much like in mySQL). Anyway, this is all guessing on that part.
but that does makes sense to me if that's what it is. I haven't started my
college course yet, so that's why I don't know this stuff, i'm sure. But,
one more question.. what does the "*" do to the "c"? I'm guessing, it's
some sort of alias pointer.. but even if that's what it is. It makes no
sense to me, why you would need an "alias" if the letter c was the variable
name?!.. I'm really confused on that part. And I know this isn't good, my
first language I learned was vb (don't tell anyone that lol, i'm rather
embarased). And, so, now it's kind of hard to accept the way new languages
do things, especially when i've never seen it before. could someone
clarify what exactly the "*" does please? thank you in advanced.
Jul 19 '05 #6

P: n/a
Mike Lundell wrote in news:vi************@corp.supernews.com:
Rob Williscroft wrote:
char[16]
is that like a new instance of the "char" class defined as char *c?


char my_array[16]; // an array of 16 char's
char *p = my_array;

p 'points to' the first element of 'my_array'.

in C++ you can reference the array's elements like this:

char c = my_array[ n ]; // where 0 <= n and n < 16.

C++ allows you to do the same with pointers (i.e 'p' above):

char c2 = p[ n ];

If you want to have an array that lives longer than the function
you create it in then you can write:

char *dynamic_p = new char[16];

dynamic_p now 'points to' an (unnamed if you like) array that the
compiler has created. To get rid of the array, once you're finnished
with it, write:

delete [] dynamic_p;

if so, i'm guessing the 16 is for how many characters are defined for
that var. (much like in mySQL). Anyway, this is all guessing on that
part. but that does makes sense to me if that's what it is. I haven't
started my college course yet, so that's why I don't know this stuff,
If you want a head start before you start your course I'd suggest
getting a book, maybe one you're going to have to get for course
anyway, It'll be a lot faster than trying to learn via usenet.
i'm sure. But, one more question.. what does the "*" do to the "c"?
It changes the type of the variable being declared.

char c; // in words: c is of type char.
char *p; // in words: p is of type pointer to char.
I'm guessing, it's some sort of alias pointer.. but even if that's
what it is. It makes no sense to me, why you would need an "alias" if
the letter c was the variable name?!.. I'm really confused on that
part. And I know this isn't good, my first language I learned was vb
(don't tell anyone that lol, i'm rather embarased). And, so, now it's
kind of hard to accept the way new languages do things, especially
when i've never seen it before. could someone clarify what exactly
the "*" does please? thank you in advanced.


This post hasn't covered 1% of "what exactly the "*" does", so get
a good book and go to that course.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #7

P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bg**********@news.freedom2surf.net>...

char* a = "* * * * * * * *\n";

bye Boris


This is wrong too - you either need

char a[] = "* * * * * * * *\n";

or

char *a = new char[16] = "* * * * * * * *\n";

Allan


or const char *a = "* * * * * * * *\n";

GJD
Jul 19 '05 #8

P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
in message news:<bg**********@news.freedom2surf.net>...
char* a = "* * * * * * * *\n";
This is wrong too - you either need


It is indeed wrong but for other reasons than those you suggest: the type
of a string literal is 'char const[n]' (where 'n' is the number of characters
occupied by the string). You thus cannot assign it to a 'char*'. Well,
actually I think you can but this is a deprecated feature. You should write

char const* a = "* * * * * * * *\n";

instead.
char a[] = "* * * * * * * *\n";
This is something quite different: the above approach just obtains a pointer
to string literal while what you are doing here is to create an array of
characters which is initialized by copying the string literal.
or
char *a = new char[16] = "* * * * * * * *\n";


.... and this is utter nonsense, supposed to be rejected by the compiler: the
result of 'new char[16]' is not an lvalue and thus you cannot assign to it.
Even if it were an lvalue it would have the wrong semantic: it would assign
the pointer to the string literal discarding the pointer to the just
allocated memory thereby creating a resource leak.

BTW, the string literal you used above has 17 characters, not 16! You forgot
the terminating null character.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 19 '05 #9

P: n/a
Rob Williscroft wrote:
char c; // in words: c is of type char.
char *p; // in words: p is of type pointer to char.


Thanks tons for the response Rob. It makes a lot more sense now. :)
Jul 19 '05 #10

P: n/a
> Allan Bruce wrote in news:bg**********@news.freedom2surf.net:

or

char *a = new char[16] = "* * * * * * * *\n";


#include <iostream>
#include <ostream>

int main()
{
char *a = "* * * * * * * *\n";
char *b = new char[16] = "* * * * * * * *\n";
char *c = new char[16];

std::cout << "a: " << ((void *)a) << std::endl;
std::cout << "b: " << ((void *)b) << std::endl;
std::cout << "c: " << ((void *)c) << std::endl;
}

The output I get (MSVC 7.1) is:

a: 0041E01C
b: 0041E030
c: 002F0F88

I was supprised that my compiler even let me write this nonsense.

Rob.


I was surprised it compiles with g++ as well, but I do get a compiler
warning.

My guess is that (with MSVC 7.1) if you print out b ( and not *b ) it
will not be what you expect, ie not "* * * * * * * * * *\n"

-Brian
Jul 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.