473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with array of structures

Could anyone please tell me what is wrong with this code? Compiler error
is: "parse error before '.' token" in lines 13-16. I tried searching the
internet but all the reference guides etc. say It's ok. Is it a problem
with the compiler? I am using the dev-c++ front-end for mingw.

/* start code */
#include <stdio.h>

typedef struct
{
int x;
int y;
int width;
int height;
}ObstacleBB;

ObstacleBB obstacles[1];

obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);

int main(void)
{
return 0;
}
/* end code */

Nov 13 '05 #1
13 2216
Ian Tuomi <ia*@co.jyu.fi> spoke thus:
ObstacleBB obstacles[1]; obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);


obstacles is a pointer to a struct (that's what you get when you declare an
array), so to access elements, you want to do obstacles[0]->x, etc.

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #2
Christopher Benson-Manica wrote:
Ian Tuomi <ia*@co.jyu.fi> spoke thus:

ObstacleBB obstacles[1];


obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);

obstacles is a pointer to a struct (that's what you get when you declare an
array), so to access elements, you want to do obstacles[0]->x, etc.


Oh ofcourse! Thankyou so much!

Nov 13 '05 #3
Ian Tuomi wrote:
Christopher Benson-Manica wrote:
Ian Tuomi <ia*@co.jyu.fi> spoke thus:

ObstacleBB obstacles[1];

obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);


obstacles is a pointer to a struct (that's what you get when you
declare an
array), so to access elements, you want to do obstacles[0]->x, etc.


Oh ofcourse! Thankyou so much!


Umh.. sorry I jumped into conclusions but now it says:
"parse error before '->' token" so looks like I either still have a
problem or mis-interperted youre message really badly.

Nov 13 '05 #4
Ian Tuomi <ia*@co.jyu.fi> wrote in
news:bk**********@phys-news1.kolumbus.fi:
Could anyone please tell me what is wrong with this code? Compiler error
is: "parse error before '.' token" in lines 13-16. I tried searching the
internet but all the reference guides etc. say It's ok. Is it a problem
with the compiler? I am using the dev-c++ front-end for mingw.

/* start code */
#include <stdio.h>

typedef struct
{
int x;
int y;
int width;
int height;
}ObstacleBB;

ObstacleBB obstacles[1];

Put these *inside* a function! You can't have run-time code hanging out
like this.
obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);

int main(void)
{
return 0;
}
/* end code */


--
- Mark ->
--
Nov 13 '05 #5

"Ian Tuomi" <ia*@co.jyu.fi> wrote in message
news:bk**********@phys-news1.kolumbus.fi...
Could anyone please tell me what is wrong with this code? Compiler error
is: "parse error before '.' token" in lines 13-16. I tried searching the
internet but all the reference guides etc. say It's ok. Is it a problem
with the compiler? I am using the dev-c++ front-end for mingw.

/* start code */
#include <stdio.h>

typedef struct
{
int x;
int y;
int width;
int height;
}ObstacleBB;

ObstacleBB obstacles[1];

obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);

This is fine, according to your structure declarations, but you need to put

obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);

inside your main function.

<snip>

int main() {

obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = 0; //(OFFSET_X * 2);
obstacles[0].height = 0; //(OFFSET_Y * 2);

return 0;
}
/* end code */

Hope it helps

int main(void)
{
return 0;
}
/* end code */

Nov 13 '05 #6
Ok, Thanks everyone it works now. wow! 3 answers in half an hour. I am
impressed. Is this normal in comp.lang.c ?

Nov 13 '05 #7
Ian Tuomi <ia*@co.jyu.fi> spoke thus:
Umh.. sorry I jumped into conclusions but now it says:
"parse error before '->' token" so looks like I either still have a
problem or mis-interperted youre message really badly.


Nah, I probably blew it - listen to the other people ;)

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #8
Ian Tuomi wrote:

obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);


As others have mentioned, you can put this code inside a function. An
alternative is to initialize your structure in its declaration.

ObstacleBB obstacles[1] =
{ /* braces around array */
{ 0, 0, OFFSET_X * 2, OFFSET_Y * 2 } /* braces around element */
};

This will only work if OFFSET_X and OFFSET_Y are #defined constants
(sloppy terminology, I know).

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================

Nov 13 '05 #9
Ian Tuomi <ia*@co.jyu.fi> wrote:
Ok, Thanks everyone it works now. wow! 3 answers in half an hour. I am
impressed. Is this normal in comp.lang.c ?


Depends on how many people are reading and posting while pretending they
are doing their job. :-)
--
The generation of random numbers is too important to be left to chance.
Nov 13 '05 #10
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Ian Tuomi <ia*@co.jyu.fi> spoke thus:
ObstacleBB obstacles[1]; obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);

obstacles is a pointer to a struct (that's what you get when you declare an
array), so to access elements, you want to do obstacles[0]->x, etc.


Nonsense. Where do you see a pointer?

Alex
Nov 13 '05 #11
Ian Tuomi <ia*@co.jyu.fi> wrote in
news:bk**********@phys-news1.kolumbus.fi:
Ok, Thanks everyone it works now. wow! 3 answers in half an hour. I am
impressed. Is this normal in comp.lang.c ?


It depends upon how long my builds are taking at the moment.

--
- Mark ->
--
Nov 13 '05 #12
Groovy hepcat Christopher Benson-Manica was jivin' on Fri, 19 Sep 2003
16:49:07 +0000 (UTC) in comp.lang.c.
Re: Problem with array of structures's a cool scene! Dig it!
Ian Tuomi <ia*@co.jyu.fi> spoke thus:
ObstacleBB obstacles[1];

obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);


obstacles is a pointer to a struct (that's what you get when you declare an
array), so to access elements, you want to do obstacles[0]->x, etc.


Absolute piffle! obstacles is an ARRAY of struct. He is accessing
the first (and only) element of this array with [0], thus yeilding a
struct. He is then correctly accessing members of this struct with the
dot operator. The error is not how he's doing that, but where.
Please refrain from posting an answer when you don't know the
correct one.

Nov 13 '05 #13
C language does not support such assignment of the global variabe.
What you can do is just move the initialization part inside main.
like
int main(void)
{
obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = 2;
obstacles[0].height = 2;
return 0;
}
or the second method can be
ObstacleBB obstacles[1] = {2,2,(OFFSET_X * 2),(OFFSET_Y * 2)};

DG
__________________________________________________ _
Ian Tuomi <ia*@co.jyu.fi> wrote in message news:<bk**********@phys-news1.kolumbus.fi>...
Could anyone please tell me what is wrong with this code? Compiler error
is: "parse error before '.' token" in lines 13-16. I tried searching the
internet but all the reference guides etc. say It's ok. Is it a problem
with the compiler? I am using the dev-c++ front-end for mingw.

/* start code */
#include <stdio.h>

typedef struct
{
int x;
int y;
int width;
int height;
}ObstacleBB;

ObstacleBB obstacles[1];

obstacles[0].x = 0;
obstacles[0].y = 0;
obstacles[0].width = (OFFSET_X * 2);
obstacles[0].height = (OFFSET_Y * 2);

int main(void)
{
return 0;
}
/* end code */

Nov 13 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve | last post by:
Hello, I created a public Structure in a Standard Module and also an array of Structures. Then I load data into the array of structures in a public sub that I call on the Form load event. ...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.