Can't access field in struct

Discussion in 'C++' started by JS, Mar 22, 2005.

  1. JS

    JS Guest

    I can't seem to access the field in this struct:

    #include <stdio.h>

    struct data{
    int x;
    int y;
    };

    static struct data test;
    struct data *pp = &test;

    pp->x = 23;





    in the last line I get "parse error before "->" token.

    How do I set x to 23?
    JS, Mar 22, 2005
    #1
    1. Advertising

  2. JS wrote:
    > I can't seem to access the field in this struct:
    >
    > #include <stdio.h>
    >
    > struct data{
    > int x;
    > int y;
    > };
    >
    > static struct data test;
    > struct data *pp = &test;
    >
    > pp->x = 23;
    >
    >
    >
    >
    >
    > in the last line I get "parse error before "->" token.
    >
    > How do I set x to 23?


    The code you posted is not a valid C++ program. Please post the
    real code you're having the problem compiling.

    V
    Victor Bazarov, Mar 22, 2005
    #2
    1. Advertising

  3. JS

    Artie Gold Guest

    JS wrote:
    > I can't seem to access the field in this struct:
    >
    > #include <stdio.h>
    >
    > struct data{
    > int x;
    > int y;
    > };
    >
    > static struct data test;
    > struct data *pp = &test;
    >
    > pp->x = 23;
    >
    >
    >
    >
    >
    > in the last line I get "parse error before "->" token.
    >
    > How do I set x to 23?
    >
    >

    No way to tell from what you've posted. Please post a small
    *compilable*[1] example.

    HTH,
    --ag

    [1] Or one you think *should* be compilable. ;-)

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, Mar 22, 2005
    #3
  4. JS

    JS Guest

    "Victor Bazarov" <> skrev i en meddelelse
    news:lU%%d.55413$01.us.to.verio.net...
    > JS wrote:
    > > I can't seem to access the field in this struct:
    > >
    > > #include <stdio.h>
    > >
    > > struct data{
    > > int x;
    > > int y;
    > > };
    > >
    > > static struct data test;
    > > struct data *pp = &test;
    > >
    > > pp->x = 23;
    > >
    > >
    > >
    > >
    > >
    > > in the last line I get "parse error before "->" token.
    > >
    > > How do I set x to 23?



    This is just it. But after trying a few things I found out its only possible
    to access a field from a method.:

    #include <stdio.h>

    struct data {

    int x;

    int y;

    };

    struct data test;

    main(){

    test.x = 45;


    printf("%d\n", test.x);

    printf("%d\n", test.y);


    getchar();



    }



    Now it works
    JS, Mar 22, 2005
    #4
  5. JS wrote:
    > "Victor Bazarov" <> skrev i en meddelelse
    > news:lU%%d.55413$01.us.to.verio.net...
    >
    >>JS wrote:
    >>
    >>>I can't seem to access the field in this struct:
    >>>
    >>>#include <stdio.h>
    >>>
    >>>struct data{
    >>> int x;
    >>> int y;
    >>>};
    >>>
    >>> static struct data test;
    >>> struct data *pp = &test;
    >>>
    >>> pp->x = 23;
    >>>
    >>>
    >>>
    >>>
    >>>
    >>>in the last line I get "parse error before "->" token.
    >>>
    >>>How do I set x to 23?

    >
    >
    >
    > This is just it. But after trying a few things I found out its only possible
    > to access a field from a method.:
    >
    > #include <stdio.h>
    >
    > struct data {
    >
    > int x;
    >
    > int y;
    >
    > };
    >
    > struct data test;


    "struct" is superfluous here. This is C++, not C. Now, for the fun
    of it, add

    data *ptest = &test;

    >
    > main(){


    Change to

    int main() {

    otherwise it's not C++.

    >
    > test.x = 45;


    Replace this with

    ptest->x = 45;

    and you should have exactly same effect.

    >
    >
    > printf("%d\n", test.x);
    >
    > printf("%d\n", test.y);
    >
    >
    > getchar();
    >
    >
    >
    > }
    >
    >
    >
    > Now it works


    What it was that didn't work before we'll never know. See the FAQ,
    section 5.

    V
    Victor Bazarov, Mar 22, 2005
    #5
  6. "Victor Bazarov" <> wrote in message
    news:6x00e.55534$01.us.to.verio.net...
    >
    > Perhaps. Do we actually care?


    It depends on who you ask.

    - JFA1
    James Aguilar, Mar 22, 2005
    #6
  7. JS

    Howard Guest

    "Victor Bazarov" <> wrote in message
    news:y700e.55462$01.us.to.verio.net...

    > What it was that didn't work before we'll never know. See the FAQ,
    > section 5.
    >
    > V


    Judging from his comment: "I found out its only possible to access a field
    from a method", perhaps he didn't have that code inside main?

    -Howard
    Howard, Mar 22, 2005
    #7
  8. Howard wrote:
    > "Victor Bazarov" <> wrote in message
    > news:y700e.55462$01.us.to.verio.net...
    >
    >
    >>What it was that didn't work before we'll never know. See the FAQ,
    >>section 5.
    >>
    >>V

    >
    >
    > Judging from his comment: "I found out its only possible to access a field
    > from a method", perhaps he didn't have that code inside main?


    Perhaps. Do we actually care?
    Victor Bazarov, Mar 22, 2005
    #8
    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.

Share This Page