Stroustrup 5.9, exercise 1

Discussion in 'C++' started by arnuld, Mar 30, 2007.

  1. arnuld

    arnuld Guest

    ------ PROGRAMME -----------
    /* Stroustrup, 5.9, exercises

    to write some declarations (with initializers)
    comments explain what we intend to do
    */


    #include<iostream>


    int main()
    {

    // a pointer to a character
    char c1 = 'a';
    char* pc1 = &c1;

    // an array of 10 integers
    const int arr_size = 10;
    char arr_int[arr_size];

    // a reference to an array of 10 integers
    char& r_arr_int = arr_int;

    // a pointer to an array of character strings
    /* SORRY but i do not know how to present an array
    of character strings */

    // a pointer to a pointer to a char
    char c;
    char* pc = &c;
    char* ppc = pc;

    // a constant integer
    const int ci = 7;

    // a pointer to a constant integer
    const int* pci = &ci;

    // a constant pointer to an integer
    int j = 8;
    int *const cpi = &j;


    return 0;
    }

    --------- OUTPUT ---------------
    [arch@voodo tc++pl]$ g++ 5.9_ex-01.cpp
    5.9_ex-01.cpp: In function 'int main()':
    5.9_ex-01.cpp:23: error: invalid initialization of non-const reference
    of type 'char&' from a temporary of type 'char*'
    [arch@voodo tc++pl]$
    -----------------------------------

    i did not use "-ansi -W" flags as they were emitting warnings i did
    not need to correct at this moment. i know that this is an error:

    // a reference to an array of 10 integers
    char& r_arr_int = arr_int;

    i dont find any way to create a reference to an integer. BTW, are all
    declarations fine ?
     
    arnuld, Mar 30, 2007
    #1
    1. Advertising

  2. * arnuld:
    >
    > i know that this is an error:
    >
    > // a reference to an array of 10 integers
    > char& r_arr_int = arr_int;
    >
    > i dont find any way to create a reference to an integer. BTW, are all
    > declarations fine ?


    What you have above is a reference to char, initialized with an array of
    integers.

    Apples on one side, oranges on the other side.

    This is a reference to an array:

    int (&r_arr_int)[10] = arr_int;


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 30, 2007
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    > On Mar 30, 7:42 pm, "Alf P. Steinbach" <> wrote:
    > * arnuld:



    > > // a reference to an array of 10 integers
    > > char& r_arr_int = arr_int;


    > What you have above is a reference to char, initialized with an array of
    > integers.
    >
    > Apples on one side, oranges on the other side.
    >
    > This is a reference to an array:
    >
    > int (&r_arr_int)[10] = arr_int;


    it does not compile:

    ---------- PROGRAMME -------------
    /* Stroustrup, 5.9, exercises

    to write declarations with initializers.
    comments explain what we intend to do.

    */


    #include<iostream>


    int main()
    {

    // a pointer to a character
    char c1 = 'a';
    char* pc1 = &c1;

    // an array of 10 integers
    const int arr_size = 10;
    char arr_int[arr_size];

    // a reference to an array of 10 integers
    int (&r_arr_int)[arr_size] = arr_int;

    // a pointer to an array of character strings
    /* SORRY but i do not know how to present an array
    of character strings */

    // a pointer to a pointer to a char
    char c;
    char* pc = &c;
    char* ppc = pc;

    // a constant integer
    const int ci = 7;

    // a pointer to a constant integer
    const int* pci = &ci;

    // a constant pointer to an integer
    int j = 8;
    int *const cpi = &j;


    return 0;
    }

    -------- OUTPUT ----------
    [arch@voodo tc++pl]$ g++ 5.9_ex-01.cpp
    5.9_ex-01.cpp: In function 'int main()':
    5.9_ex-01.cpp:24: error: invalid initialization of reference of type
    'int (&)[10]' from expression of type 'char [10]'
    [arch@voodo tc++pl]$
     
    arnuld, Mar 30, 2007
    #3
  4. * arnuld:
    > 5.9_ex-01.cpp: In function 'int main()':
    > 5.9_ex-01.cpp:24: error: invalid initialization of reference of type
    > 'int (&)[10]' from expression of type 'char [10]'
    > [arch@voodo tc++pl]$


    Well, what does that tell you? 'int' on one side, 'char' on the other
    side, they're not the same.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 30, 2007
    #4
  5. arnuld

    arnuld Guest

    > On Mar 30, 9:19 pm, "Alf P. Steinbach" <> wrote:


    > Well, what does that tell you? 'int' on one side, 'char' on the other
    > side, they're not the same.


    really very SORRY for the stupidity i have done
     
    arnuld, Mar 30, 2007
    #5
  6. arnuld

    Gavin Deane Guest

    On 30 Mar, 16:00, "arnuld" <> wrote:
    > // a pointer to a pointer to a char
    > char c;
    > char* pc = &c;
    > char* ppc = pc;


    No, that's not right. Reading the declaration right to left again:

    char* ppc
    ^ ^ ^
    3 2 1

    ppc [1] is a pointer to [2] char [3]. You wanted a pointer to a
    pointer to a char, which is different.

    You initialised it as a copy of pc. So now you have two different
    pointers-to-char (pc and ppc) that currently both happen to point to
    the same char (c).

    * in the type declaration means "pointer to", and the type you want to
    declare includes the phrase "pointer to" twice.

    Gavin Deane
     
    Gavin Deane, Mar 30, 2007
    #6
  7. arnuld

    arnuld Guest

    > On Mar 30, 10:21 pm, "Gavin Deane" <> wrote:

    > On 30 Mar, 16:00, "arnuld" <> wrote:
    >
    > > // a pointer to a pointer to a char
    > > char c;
    > > char* pc = &c;
    > > char* ppc = pc;

    >
    > No, that's not right. Reading the declaration right to left again:
    >
    > char* ppc
    > ^ ^ ^
    > 3 2 1
    >
    > ppc [1] is a pointer to [2] char [3]. You wanted a pointer to a
    > pointer to a char, which is different.
    >
    > You initialised it as a copy of pc. So now you have two different
    > pointers-to-char (pc and ppc) that currently both happen to point to
    > the same char (c).
    >
    > * in the type declaration means "pointer to", and the type you want to
    > declare includes the phrase "pointer to" twice.
    >
    > Gavin Deane


    i must call it a BUG (arose of typing) what i actually meant was:

    char c;
    char* pc = &c;
    char** ppc = &pc;

    and it compiles without any trouble
     
    arnuld, Mar 30, 2007
    #7
  8. arnuld

    Gavin Deane Guest

    On 30 Mar, 18:32, "arnuld" <> wrote:
    > i must call it a BUG (arose of typing) what i actually meant was:
    >
    > char c;
    > char* pc = &c;
    > char** ppc = &pc;
    >
    > and it compiles without any trouble


    And does what you want it to. Looks like I was explaining something
    you already knew.

    Gavin Deane
     
    Gavin Deane, Mar 30, 2007
    #8
  9. arnuld

    Marcus Kwok Guest

    arnuld <> wrote:
    > // a pointer to an array of character strings
    > /* SORRY but i do not know how to present an array
    > of character strings */


    Let me know if you understand this:


    #include <iostream>

    int main()
    {
    const int array_size = 4;

    char* char_string_array[array_size] = {"one", "two", "three", "four"};

    // a pointer to an array of character strings
    char* (*pacs)[array_size] = &char_string_array;

    for (int i = 0; i != array_size; ++i) {
    std::cout << pacs << ": " << (*pacs) << '\n';
    }
    }



    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Apr 3, 2007
    #9
  10. arnuld

    Old Wolf Guest

    On Apr 4, 2:39 am, (Marcus Kwok) wrote:
    > Let me know if you understand this:
    >
    > #include <iostream>
    >
    > int main()
    > {
    > const int array_size = 4;
    >
    > char* char_string_array[array_size] = {"one", "two", "three", "four"};
    >
    > // a pointer to an array of character strings
    > char* (*pacs)[array_size] = &char_string_array;
    >
    > for (int i = 0; i != array_size; ++i) {
    > std::cout << pacs << ": " << (*pacs) << '\n';
    > }
    > }


    Did you mean to cause undefined behaviour?

    (Also, you should use 'const char*' rather than 'char*').
     
    Old Wolf, Apr 4, 2007
    #10
  11. arnuld

    Marcus Kwok Guest

    Old Wolf <> wrote:
    > On Apr 4, 2:39 am, (Marcus Kwok) wrote:
    >> Let me know if you understand this:
    >>
    >> #include <iostream>
    >>
    >> int main()
    >> {
    >> const int array_size = 4;
    >>
    >> char* char_string_array[array_size] = {"one", "two", "three", "four"};
    >>
    >> // a pointer to an array of character strings
    >> char* (*pacs)[array_size] = &char_string_array;
    >>
    >> for (int i = 0; i != array_size; ++i) {
    >> std::cout << pacs << ": " << (*pacs) << '\n';
    >> }
    >> }

    >
    > Did you mean to cause undefined behaviour?


    No, I did not. What did I do wrong?

    > (Also, you should use 'const char*' rather than 'char*').


    True.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Apr 4, 2007
    #11
  12. arnuld

    Marcus Kwok Guest

    Marcus Kwok <> wrote:
    > Old Wolf <> wrote:
    >> Did you mean to cause undefined behaviour?

    >
    > No, I did not. What did I do wrong?


    OK, after looking at the code more closely, I think the reference to
    pacs is what is causing the UB.

    >> (Also, you should use 'const char*' rather than 'char*').

    >
    > True.


    Here is a revised program:


    #include <iostream>

    int main()
    {
    const int array_size = 4;

    const char* char_string_array[array_size] = {"one", "two", "three", "four"};

    // a pointer to an array of character strings
    const char* (*pacs)[array_size] = &char_string_array;

    for (int i = 0; i != array_size; ++i) {
    std::cout << (*pacs) << '\n';
    }
    }


    This one look OK?

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Apr 4, 2007
    #12
  13. arnuld

    Old Wolf Guest

    On Apr 5, 9:25 am, (Marcus Kwok) wrote:
    >
    > OK, after looking at the code more closely, I think the reference to
    > pacs is what is causing the UB. Here is a revised program:
    >
    > for (int i = 0; i != array_size; ++i) {
    > std::cout << (*pacs) << '\n';
    > }
    >
    > This one look OK?


    Much better :)
     
    Old Wolf, Apr 5, 2007
    #13
    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. arnuld

    Stroustrup 5.9 exercise 6

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    7
    Views:
    388
    arnuld
    Nov 8, 2006
  2. arnuld
    Replies:
    5
    Views:
    369
    Alf P. Steinbach
    Nov 8, 2006
  3. arnuld
    Replies:
    2
    Views:
    318
    arnuld
    Nov 8, 2006
  4. arnuld
    Replies:
    6
    Views:
    401
    arnuld
    Nov 8, 2006
  5. arnuld

    Stroustrup 5.9 exercise 9

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    5
    Views:
    425
    Bernd Strieder
    Nov 9, 2006
Loading...

Share This Page