Extremely Simple Question

Discussion in 'C++' started by Mike Lundell, Jul 29, 2003.

  1. Mike Lundell

    Mike Lundell Guest

    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
     
    Mike Lundell, Jul 29, 2003
    #1
    1. Advertising

  2. Mike Lundell

    Boris Glawe Guest

    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
     
    Boris Glawe, Jul 30, 2003
    #2
    1. Advertising

  3. Mike Lundell

    Mike Lundell Guest

    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.
     
    Mike Lundell, Jul 30, 2003
    #3
  4. 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
     
    Karl Heinz Buchegger, Jul 30, 2003
    #4
  5. Allan Bruce wrote in news:bg87sr$gap$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/
     
    Rob Williscroft, Jul 30, 2003
    #5
  6. Mike Lundell

    Mike Lundell Guest

    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.
     
    Mike Lundell, Jul 30, 2003
    #6
  7. Mike Lundell wrote in news::

    > 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/
     
    Rob Williscroft, Jul 30, 2003
    #7
  8. Mike Lundell

    Gavin Deane Guest

    "Allan Bruce" <> wrote in message news:<bg87sr$gap$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
     
    Gavin Deane, Jul 30, 2003
    #8
  9. "Allan Bruce" <> wrote:
    in message news:<bg87sr$gap$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.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
     
    Dietmar Kuehl, Jul 30, 2003
    #9
  10. Mike Lundell

    Mike Lundell Guest

    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. :)
     
    Mike Lundell, Jul 30, 2003
    #10
  11. Mike Lundell

    Big Brian Guest

    > Allan Bruce wrote in news:bg87sr$gap$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.


    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
     
    Big Brian, Jul 30, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loquak

    Extremely simple XML

    loquak, Apr 11, 2004, in forum: Java
    Replies:
    5
    Views:
    423
    Sunwest Technologies
    Apr 12, 2004
  2. Mike
    Replies:
    4
    Views:
    480
  3. Mike Lundell

    extremely simple question

    Mike Lundell, Jul 29, 2003, in forum: C++
    Replies:
    1
    Views:
    276
    Tim Slattery
    Jul 30, 2003
  4. Aaron Watters
    Replies:
    26
    Views:
    578
    Aaron Watters
    Jan 6, 2004
  5. James

    Reporting Woes (extremely simple)

    James, Aug 4, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    242
    Munna
    Aug 5, 2008
Loading...

Share This Page