Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Extremely Simple Question

Reply
Thread Tools

Extremely Simple Question

 
 
Mike Lundell
Guest
Posts: n/a
 
      07-29-2003
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
 
Reply With Quote
 
 
 
 
Boris Glawe
Guest
Posts: n/a
 
      07-30-2003
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

 
Reply With Quote
 
 
 
 
Mike Lundell
Guest
Posts: n/a
 
      07-30-2003
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.
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-30-2003


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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      07-30-2003
Allan Bruce wrote in news:bg87sr$gap$(E-Mail Removed)2surf.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/
 
Reply With Quote
 
Mike Lundell
Guest
Posts: n/a
 
      07-30-2003
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.
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      07-30-2003
Mike Lundell wrote in news:(E-Mail Removed):

> 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/
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      07-30-2003
"Allan Bruce" <(E-Mail Removed)> wrote in message news:<bg87sr$gap$(E-Mail Removed)2surf.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
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      07-30-2003
"Allan Bruce" <(E-Mail Removed)> wrote:
in message news:<bg87sr$gap$(E-Mail Removed)2surf.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.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
 
Reply With Quote
 
Mike Lundell
Guest
Posts: n/a
 
      07-30-2003
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extremely simple printf question Wes Gamble Ruby 1 09-29-2006 07:23 PM
extremely simple xml parser? Mike Java 4 06-18-2004 12:40 PM
Extremely simple XML loquak Java 5 04-12-2004 03:27 PM
ANNOUNCE: xsdb -- the eXtremely Simple Database goes alpha Aaron Watters Python 26 01-06-2004 02:31 PM
extremely simple question Mike Lundell C++ 1 07-30-2003 12:56 PM



Advertisments