By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

Can't access field in struct

P: n/a
JS
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?
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
JS

"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:lU*******************@newsread1.mlpsca01.us.t o.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

Jul 23 '05 #4

P: n/a
JS wrote:
"Victor Bazarov" <v.********@comAcast.net> skrev i en meddelelse
news:lU*******************@newsread1.mlpsca01.us.t o.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
Jul 23 '05 #5

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:y7*******************@newsread1.mlpsca01.us.t o.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

Jul 23 '05 #6

P: n/a
Howard wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:y7*******************@newsread1.mlpsca01.us.t o.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?
Jul 23 '05 #7

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:6x*******************@newsread1.mlpsca01.us.t o.verio.net...

Perhaps. Do we actually care?


It depends on who you ask.

- JFA1
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.