problem with accessing a "struct" using "->"

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

  1. arnuld

    arnuld Guest

    -------- PROGRAMME -----------
    /* Stroustrup, 5.6 Structures

    STATEMENT:
    this programmes *tries* to do do this in 3 parts:

    1.) it creates a "struct", named "jd", of type "address".
    2. it then adds values to "jd"
    3.) in the end it prints values of "jd".

    */

    #include<iostream>
    #include<vector>

    struct address;
    void fill_addr(address); // assigns values to a struct of type
    "address"
    void print_addr(address*); // prints an address struct

    struct address {
    char* name;
    char* country;
    };



    int main()
    {
    address jd; // an "address" struct
    fill_addr(jd);
    print_addr(jd);

    return 0;
    }


    struct* fill_addr(address* jd)
    {
    jd.name = "Niklaus Wirth";
    jd.country = "Switzerland";

    return jd;
    }

    void print_addr(address* p)
    {
    using std::cout;
    using std::endl;
    cout << p->name
    << '\n'
    << p->country
    << endl;
    }

    ----------- OUTPUT --------------
    [arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra
    5.6_structures.cpp
    5.6_structures.cpp: In function 'int main()':
    5.6_structures.cpp:30: error: cannot convert 'address' to 'address*'
    for argument '1' to 'void print_addr(address*)'
    5.6_structures.cpp: At global scope:
    5.6_structures.cpp:36: error: expected identifier before '*' token
    5.6_structures.cpp: In function 'int* fill_addr(address*)':
    5.6_structures.cpp:38: error: request for member 'name' in 'jd', which
    is of non-class type 'address*'
    5.6_structures.cpp:39: error: request for member 'country' in 'jd',
    which is of non-class type 'address*'
    5.6_structures.cpp:41: error: cannot convert 'address*' to 'int*' in
    return
    [arch@voodo tc++pl]$


    i know the error at "line 30" means but i am not able to correct it. i
    tried with different ways of using "address" and "address*" but it
    does not work :-(
    arnuld, Mar 29, 2007
    #1
    1. Advertising

  2. * arnuld:
    >
    > i know the error at "line 30" means but i am not able to correct it. i
    > tried with different ways of using "address" and "address*" but it
    > does not work :-(


    Try the address operator, "&".



    --
    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 29, 2007
    #2
    1. Advertising

  3. arnuld

    neurorebel Guest

    I guess your fill_addr function should be something like this

    void fill_addr(address& jd)
    {
    jd.name = "Niklaus Wirth";
    jd.country = "Switzerland";
    }


    Prefer reference parameters when you want to modify a variable inside
    a function. And by the way you must initialize the strings in your
    struct. I recommend you to use c++ strings and your struct would look
    better this way

    struct address {
    string name;
    string country;

    };

    And let's change the print_addr function to be consistent with the
    aboce modifications

    void print_addr(const address p) // print_addr doesn't need to change
    the contents of p
    {
    using std::cout;
    using std::endl;
    cout << p.name
    << '\n'
    << p.country
    << endl;
    }
    neurorebel, Mar 29, 2007
    #3
  4. arnuld

    Gavin Deane Guest

    On 29 Mar, 11:03, "neurorebel" <> wrote:
    > void print_addr(const address p) // print_addr doesn't need to change
    > the contents of p
    > {
    > using std::cout;
    > using std::endl;
    > cout << p.name
    > << '\n'
    > << p.country
    > << endl;
    > }


    Maybe it's what you meant, but if you go this way I see no reason why
    print_addr shouldn't take a const reference
    void print_addr(const address& p) { ... }

    Gavin Deane
    Gavin Deane, Mar 29, 2007
    #4
  5. arnuld

    arnuld Guest

    > On Mar 29, 3:31 pm, "Gavin Deane" <> wrote:

    > > void print_addr(const address p) // print_addr doesn't need to change
    > > the contents of p


    good idea :)


    > > {
    > > using std::cout;
    > > using std::endl;
    > > cout << p.name
    > > << '\n'
    > > << p.country
    > > << endl;
    > > }

    >
    > Maybe it's what you meant, but if you go this way I see no reason why
    > print_addr shouldn't take a const reference
    > void print_addr(const address& p) { ... }


    it takes a /const reference/ , no problem.

    my programme works now, fine,no troubles. THANKS:

    ------------ PROGRAMME -------------
    /* Stroustrup, 5.6 Structures

    STATEMENT:
    this programmes has 3 parts:

    1.) it creates a "struct", named "jd", of type "address".
    2. it then adds values to "jd"
    3.) in the end it prints values of "jd".

    */

    #include<iostream>
    #include<vector>

    struct address;
    void fill_addr(address&); // assigns values to a struct of type
    "address"
    void print_addr(const address&); // prints an address struct

    struct address {
    char* name;
    char* country;
    };



    int main()
    {
    address jd; // an "address" struct
    address& ref_jd = jd;
    fill_addr(ref_jd);
    print_addr(ref_jd);

    return 0;
    }


    void fill_addr(address& jd)
    {
    jd.name = "Niklaus Wirth";
    jd.country = "Switzerland";

    }

    void print_addr(const address& p)
    {
    using std::cout;
    using std::endl;
    cout << p.name
    << '\n'
    << p.country
    << endl;
    }

    -------- OUTPUT -------------

    [arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra
    5.6_structures.cpp
    [arch@voodo tc++pl]$ ./a.out
    Niklaus Wirth
    Switzerland
    [arch@voodo tc++pl]$
    arnuld, Mar 29, 2007
    #5
  6. arnuld

    arnuld Guest

    On Mar 29, 3:03 pm, "neurorebel" <> wrote:
    > I guess your fill_addr function should be something like this
    >
    > void fill_addr(address& jd)
    > {
    > jd.name = "Niklaus Wirth";
    > jd.country = "Switzerland";
    >
    > }
    >
    > Prefer reference parameters when you want to modify a variable inside
    > a function. And by the way you must initialize the strings in your
    > struct. I recommend you to use c++ strings and your struct would look
    > better this way
    >
    > struct address {
    > string name;
    > string country;
    >
    > };
    >
    > And let's change the print_addr function to be consistent with the
    > aboce modifications
    >
    > void print_addr(const address p) // print_addr doesn't need to change
    > the contents of p
    > {
    > using std::cout;
    > using std::endl;
    > cout << p.name
    > << '\n'
    > << p.country
    > << endl;
    >
    > }


    thanks, it works
    arnuld, Mar 29, 2007
    #6
  7. arnuld

    Gavin Deane Guest

    On 29 Mar, 11:45, "arnuld" <> wrote:
    > ------------ PROGRAMME -------------
    > /* Stroustrup, 5.6 Structures
    >
    > STATEMENT:
    > this programmes has 3 parts:
    >
    > 1.) it creates a "struct", named "jd", of type "address".
    > 2. it then adds values to "jd"
    > 3.) in the end it prints values of "jd".
    >
    > */
    >
    > #include<iostream>
    > #include<vector>
    >
    > struct address;
    > void fill_addr(address&); // assigns values to a struct of type
    > "address"
    > void print_addr(const address&); // prints an address struct
    >
    > struct address {
    > char* name;
    > char* country;
    >
    > };
    >
    > int main()
    > {
    > address jd; // an "address" struct
    > address& ref_jd = jd;
    > fill_addr(ref_jd);
    > print_addr(ref_jd);
    >
    > return 0;
    >
    > }


    Glad you got it working. You might know this, but note that there is
    no need for ref_jd in your main function. This variation has identical
    effect:

    int main()
    {
    address jd;
    fill_addr(jd);
    print_addr(jd);

    return 0;
    }

    Gavin Deane
    Gavin Deane, Mar 29, 2007
    #7
  8. arnuld

    arnuld Guest

    > On Mar 29, 4:37 pm, "Gavin Deane" <> wrote:


    > Glad you got it working.


    :)

    > You might know this, but note that there is
    > no need for ref_jd in your main function. This variation has identical
    > effect:


    i did not know this.

    > int main()
    > {
    > address jd;
    > fill_addr(jd);
    > print_addr(jd);
    >
    > return 0;
    >
    > }


    yes, it runs.

    Gavin, it means, it is an "implicit conversion" to a "reference".
    isn't using "explicit references" a good coding practice ?
    arnuld, Mar 29, 2007
    #8
  9. arnuld

    Gavin Deane Guest

    On 29 Mar, 13:20, "arnuld" <> wrote:
    > > On Mar 29, 4:37 pm, "Gavin Deane" <> wrote:
    > > Glad you got it working.

    >
    > :)
    >
    > > You might know this, but note that there is
    > > no need for ref_jd in your main function. This variation has identical
    > > effect:

    >
    > i did not know this.
    >
    > > int main()
    > > {
    > > address jd;
    > > fill_addr(jd);
    > > print_addr(jd);

    >
    > > return 0;

    >
    > > }

    >
    > yes, it runs.
    >
    > Gavin, it means, it is an "implicit conversion" to a "reference".
    > isn't using "explicit references" a good coding practice ?


    There is no conversion. References are not objects in their own right.
    A reference simply gives you an alias for an existing object. Whenever
    you use the name of the reference in code, it is as if you had used
    the name of the object referred to. So when you do fill_addr(jd) in
    main, the reference-to-address parameter of the fill_addr function is
    initialised to refer to the object jd.

    In your original code you had this in your main function

    address jd; // an "address" struct
    address& ref_jd = jd; // <<-- LINE 2
    fill_addr(ref_jd); // <<-- LINE 3

    On the line I have marked as Line 2, ref_jd is initialised to refer to
    the object jd. From then on, wherever you write ref_jd, it is as if
    you had written jd. So on the line I have marked as Line 3, what is
    the reference-to-address parameter of the fill_addr function
    initialised to refer to now? It doesn't refer to ref_jd. That's
    meaningless. ref_jd isn't an object - it isn't a thing that can be
    referred to. ref_jd is simply an alias for jd. There is no such thing
    as a reference to a reference. A reference has to refer to some
    object, and the object in question here is jd. So the reference-to-
    address parameter of the fill_addr function is initialised to refer to
    the object jd. Exactly as before.

    ref_jd is entirely redundant and serves only to clutter your code and
    potentially confuse a reader.

    Gavin Deane
    Gavin Deane, Mar 29, 2007
    #9
  10. arnuld

    arnuld Guest

    what exactly is a Reference (was: problem accessing "struct" using "->")

    > On Mar 29, 5:38 pm, "Gavin Deane" <> wrote:


    > There is no conversion. References are not objects in their own right.
    > A reference simply gives you an alias for an existing object. Whenever
    > you use the name of the reference in code, it is as if you had used
    > the name of the object referred to. So when you do fill_addr(jd) in
    > main, the reference-to-address parameter of the fill_addr function is
    > initialised to refer to the object jd.


    it is confusing to me *What* exactly a reference is ? it is not an
    object (means an area of memory) but it still exists. where does it
    exist ?

    [FAQ]
    i see the FAQ has an article on it: http://www.parashift.com/c -faq-lite/references.html

    it says:

    "A reference is the object. It is not a pointer to the object, nor a
    copy of the object. It is the object."

    if it is the object then why does it takes the address at one time and
    increments the value some other time:

    swap(int& i, int& j)

    gets the address but

    i++, increments the value. i am not a C programmer but i did 1st
    chapter of K&R2 where i can see that to get address we use "*i" and to
    increment value we use "(*i)++".

    [/FAQ]

    does it mean "references" are implemented as "pointers" in the core
    language ?


    > In your original code you had this in your main function
    >
    > address jd; // an "address" struct
    > address& ref_jd = jd; // <<-- LINE 2
    > fill_addr(ref_jd); // <<-- LINE 3
    >
    > On the line I have marked as Line 2, ref_jd is initialised to refer to
    > the object jd. From then on, wherever you write ref_jd, it is as if
    > you had written jd. So on the line I have marked as Line 3, what is
    > the reference-to-address parameter of the fill_addr function
    > initialised to refer to now?


    it will refer to "jd", the original object.


    > It doesn't refer to ref_jd.


    yes.



    > That's meaningless.


    i dont get it. i am not saying that it is "meaningless" i am saying,
    it is OK if it is meaningless and is this how we use References. if
    yes, then it means we will never use pointers in C++, because
    References fulfill the job of taking address and dereference
    automatically.


    > ref_jd isn't an object - it isn't a thing that can be
    > referred to. ref_jd is simply an alias for jd. There is no such thing
    > as a reference to a reference. A reference has to refer to some
    > object, and the object in question here is jd. So the reference-to-
    > address parameter of the fill_addr function is initialised to refer to
    > the object jd. Exactly as before.


    out of my head :-(

    > ref_jd is entirely redundant and serves only to clutter your code and
    > potentially confuse a reader.


    that i understand pretty well, i will never do that again :)

    > Gavin Deane


    :)
    arnuld, Mar 29, 2007
    #10
  11. Re: what exactly is a Reference

    * arnuld:
    >
    > does it mean "references" are implemented as "pointers" in the core
    > language ?


    It means references are /probably/ implemented as pointers (memory
    addresses), but you have no way of knowing other than looking at the
    generated machine code.

    At the C++ level a reference is simply a synonym.

    By the way, I think you should write a new version of your "fill in
    address" program, this time using pointer arguments. It's better to use
    reference arguments for this program, yes without a doubt. But it's
    also a Good Idea(TM) to master the basics of pointers.

    --
    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 29, 2007
    #11
  12. arnuld

    arnuld Guest

    Re: what exactly is a Reference

    > On Mar 29, 7:23 pm, "Alf P. Steinbach" <> wrote:

    > > does it mean "references" are implemented as "pointers" in the core
    > > language ?

    >
    > It means references are /probably/ implemented as pointers (memory
    > addresses), but you have no way of knowing other than looking at the
    > generated machine code.


    probably, :-(

    and no way except of looking at machine code :-(


    > At the C++ level a reference is simply a synonym.


    that is the "synonym" that confuses me.


    > By the way, I think you should write a new version of your "fill in
    > address" program, this time using pointer arguments. It's better to use
    > reference arguments for this program, yes without a doubt. But it's
    > also a Good Idea(TM) to master the basics of pointers.


    Joel agrees with you, completely [1].

    BTW, here is my version using Pointers, it runs fine. see if you have
    any advice for improvement (except of using "const pointer" which i
    never understood).

    even then i feel, till yet, "Pointers" are easier to understand than
    using "References".

    -------------- PROGRAMME ----------
    #include<iostream>

    struct address;
    void fill_addr(address*); // assigns values to a struct of type
    "address"
    void print_addr(address*); // prints an address struct

    struct address {
    char* name;
    char* country;
    };



    int main()
    {
    address jd; // an "address" struct
    address* pjd = &jd;
    fill_addr(pjd);
    print_addr(pjd);

    return 0;
    }


    void fill_addr(address* jd)
    {
    jd->name = "Niklaus Wirth";
    jd->country = "Switzerland";

    }

    void print_addr(address* p)
    {
    using std::cout;
    using std::endl;
    cout << p->name
    << '\n'
    << p->country
    << endl;
    }

    ------------ OUTPUT ------------
    [arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra
    5.6_structures-2.cpp
    [arch@voodo tc++pl]$ ./a.out
    Niklaus Wirth
    Switzerland
    [arch@voodo tc++pl]$
    arnuld, Mar 29, 2007
    #12
  13. arnuld

    arnuld Guest

    Re: what exactly is a Reference

    > On Mar 29, 7:38 pm, "arnuld" <> wrote:

    > BTW, here is my version using Pointers, it runs fine. see if you have
    > any advice for improvement (except of using "const pointer" which i
    > never understood).


    OK, i have tried ito understood the "const" phenomenon and this what i
    have come with :

    in both "declaration" and "definition" i changed the:

    void print_addr(address*)

    to

    void print_addr(const address*)

    this way, we are not allowed to modify the original values the pointer
    is pointing to.

    right ?
    arnuld, Mar 29, 2007
    #13
  14. arnuld

    Gavin Deane Guest

    Re: what exactly is a Reference (was: problem accessing "struct" using "->")

    On 29 Mar, 15:16, "arnuld" <> wrote:
    > > On Mar 29, 5:38 pm, "Gavin Deane" <> wrote:
    > > There is no conversion. References are not objects in their own right.
    > > A reference simply gives you an alias for an existing object. Whenever
    > > you use the name of the reference in code, it is as if you had used
    > > the name of the object referred to. So when you do fill_addr(jd) in
    > > main, the reference-to-address parameter of the fill_addr function is
    > > initialised to refer to the object jd.

    >
    > it is confusing to me *What* exactly a reference is ? it is not an
    > object (means an area of memory) but it still exists. where does it
    > exist ?


    It is a syntax concept. It exists in the code that appears on your
    screen in front of your eyes.

    > [FAQ]
    > i see the FAQ has an article on it:http://www.parashift.com/c -faq-lite/references.html
    >
    > it says:
    >
    > "A reference is the object. It is not a pointer to the object, nor a
    > copy of the object. It is the object."
    >
    > if it is the object then why does it takes the address at one time and
    > increments the value some other time:
    >
    > swap(int& i, int& j)
    >
    > gets the address but
    >
    > i++, increments the value. i am not a C programmer but i did 1st
    > chapter of K&R2 where i can see that to get address we use "*i" and to
    > increment value we use "(*i)++".
    >
    > [/FAQ]


    I'm not sure what you mean here. I don't see how addresses come into
    it. You understand that the & symbol has a two completely unrelated
    meanings, right?

    It can be used to declare a reference:
    int i;
    int& ri;

    and it is also the address-of operator used to, well, take the address
    of an object:
    int i;
    int* pi = &i;


    > does it mean "references" are implemented as "pointers" in the core
    > language ?


    They might well be. But that's a detail important only to the people
    who write the compiler.

    <snip>

    > i dont get it. i am not saying that it is "meaningless" i am saying,
    > it is OK if it is meaningless and is this how we use References. if
    > yes, then it means we will never use pointers in C++, because
    > References fulfill the job of taking address and dereference
    > automatically.


    In C++, references can be used as function parameters in many of the
    situations where you would use a pointer in C. But references are not
    a fully equivalent replacement for pointers in function parameter
    lists. Pointers can do two things references can't - they can be
    changed to point to a different object (assuming they are non-const
    pointers) and they can point to nothing - the null pointer. There is
    no such thing as a "null reference". So, if it makes sense to
    sometimes pass your function a valid object and sometimes pass it
    nothing, you can't use a reference as the parameter - you have to use
    a pointer (and check inside the function whether the pointer is
    null).

    That is the basis of "use references when you can and pointers only
    when you have to", explained in FAQ 8.6 in the link you posted.

    Gavin Deane
    Gavin Deane, Mar 29, 2007
    #14
  15. arnuld

    BobR Guest

    Re: what exactly is a Reference

    arnuld <> wrote in message ...
    >
    > OK, i have tried ito understood the "const" phenomenon and this what i
    > have come with :
    > in both "declaration" and "definition" i changed the:
    >
    > void print_addr(address*)
    > to
    > void print_addr(const address*)
    >
    > this way, we are not allowed to modify the original values the pointer
    > is pointing to. right ?


    Alf P. Steinbach's "Pointers" document:
    http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf

    --
    Bob R
    POVrookie
    BobR, Mar 30, 2007
    #15
  16. arnuld

    arnuld Guest

    Re: what exactly is a Reference

    > On Mar 30, 5:38 am, "BobR" <> wrote:

    > arnuld <> wrote in message ...
    >
    > > OK, i have tried ito understood the "const" phenomenon and this what i
    > > have come with :
    > > in both "declaration" and "definition" i changed the:

    >
    > > void print_addr(address*)
    > > to
    > > void print_addr(const address*)

    >
    > > this way, we are not allowed to modify the original values the pointer
    > > is pointing to. right ?

    >
    > Alf P. Steinbach's "Pointers" document:http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf


    :)
    arnuld, Mar 30, 2007
    #16
    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. Bharath Raghunathan

    Accessing Struct variables from JNI using SWIG

    Bharath Raghunathan, Feb 3, 2006, in forum: Java
    Replies:
    0
    Views:
    446
    Bharath Raghunathan
    Feb 3, 2006
  2. RA Scheltema
    Replies:
    3
    Views:
    380
    RA Scheltema
    Jan 6, 2004
  3. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    785
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,348
    Chris Fogelklou
    Apr 20, 2004
  5. dutchgoldtony
    Replies:
    15
    Views:
    463
    Mike Wahler
    Nov 16, 2005
Loading...

Share This Page