473,513 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't access field in struct

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
7 1948
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
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
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
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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1685
by: Mike | last post by:
Within the following structure, TopStruct, I'd like to create 3 other structures, 2 of which make up a union. The first structure will always contain some data that I need and should never be...
26
7032
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
12
3559
by: Sadeq | last post by:
Is there a way to read a struct field by field? I want to write a rather general function, like: public string EncodeStruct (struct strct) { .... } which accepts a general struct type,...
24
10291
by: funkyj | last post by:
PROBLEM STATEMENT: I want to calculate the byte offset of a field with a struct at compile time without instantiating an instance of the struct at runtime AND I want to do this in an ANSI standard...
2
40814
by: Halid Umar A M | last post by:
Dear All, Please tell me why this error is occuring. The following is the code snippets which i have typed. struct mystructure{ struct list_head m; //error: field m has incomplete...
0
1551
by: mathieu | last post by:
Hello, I am trying to use template programing and bit-field and I came up with (*). I was not able to make a really clean solution but instead separate into 3 cases: 1. mask is declare before...
6
3833
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of...
6
1261
by: castironpi | last post by:
struct.Struct lets you encode Python objects into structured memory. It accepts a format string, and optionally a buffer and offset to/from which to read/write the structure. What do you think of...
4
2179
by: James | last post by:
hello, can we have a nullable type field in the struct ? struct Employee { nullable<stringfirstName =null; } we can't initialize a field in a struct so is there any possibility that we can...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.