473,394 Members | 1,734 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,394 software developers and data experts.

Defining/dealing with a dynamical structure (C)

2
Hi,

I have a simulation program that I want to port from matlab to C. But my C has gone rusty..

I have a system (in the mathematical sense) which, at each iteration, gets in a new state. In order to determine this new state, I have to determine first what is theoretically possible, and choose among these possibilities. When the simulation starts, I have but a few possible outcomes. As the simulation progresses, the number of possible outcomes increases. From a mere dozen to several hundred thousand possibilities.. So I try to deal with these outcomes in a dynamical way.

I am trying to get all this done in a function that will return the event that took place. I define
Expand|Select|Wrap|Line Numbers
  1. typedef struct{
  2.     CITY FromCity;
  3.     CITY ToCity;
  4.     int TypeEvent;
  5.     double ProbaEvent;
  6.     double Time;
  7. } EVENT;
  8.  
in my .h, then declare
Expand|Select|Wrap|Line Numbers
  1.     EVENT *possible_events; 
in my function. Now I run (within the function) a main loop on states, and do the following (this is the first instance of possible_events being calloc/malloc/realloc-ed in any way):

Expand|Select|Wrap|Line Numbers
  1. possible_events=(EVENT *) realloc(possible_events,(nb_event+1)*sizeof(EVENT));
  2. if (possible_events == NULL)
  3. {
  4.     printf("Problem reallocating possible_events\n");
  5.     exit(1);
  6. }
  7. possible_events[nb_event]->FromCity=i;
  8.  
And get the dreaded (306 is line 7 above):
stochastic_simulation.c: In function ‘Determine_Event’:
stochastic_simulation.c:306: error: invalid type argument of ‘->’

Can anyone remind me what it is that I should/should not be doing..? Thanks.

(I should add that, being in doubt, I tried the dot dereferencing instead of the ->, and was sent packing with a different love message from gcc, something about incompatible types I believe).
Sep 10 '08 #1
2 1129
weaknessforcats
9,208 Expert Mod 8TB
possible_events=(EVENT *) realloc(possible_events,(nb_event+1)*sizeof(EVENT) );
if (possible_events == NULL)
{
printf("Problem reallocating possible_events\n");
exit(1);
}
possible_events[nb_event]->FromCity=i;
In this code possible_events is an EVENT*. That makes the array and array of EVENT. An EVENT is not a pointer so you can't use the -> with an array element.

You would have to allocate an array of EVENT** so that you could have an array of EVENT* and then possible_events[nb_event] would be an EVENT* and the -> operator would be OK. If course, you would have to populate the array with valid EVENT addresses before using possible_events[nb_event].
Sep 10 '08 #2
jarino
2
Got it, thanks a lot for the help!

(I am fine using it as an array and with the . instead of the ->, I had typecasting problems when using the .)
Sep 10 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Sune | last post by:
Hi, I need to replace some dynamical content away from a text, only static thing is that it starts with <!-- Start dynamic --> and ends with <!-- End dynamic -->. How do i dynamical remove these...
0
by: Jacob Kroon | last post by:
Hi, I'm having some problems with implementing dynamical module loading. First let me describe the scenario with an example: modules/ fruit/ __init__.py apple.py banana.py
0
by: paula | last post by:
I've got a problem with asp.net i am trying to make a menu control. and have searched the web for serveral controls but they don't work correctly. I am pretty new to asp.net building. What am i...
4
by: FraterQ | last post by:
Hello, after i've read this post, i tried it myself and it works....
5
by: mblatch | last post by:
Another basic C# question, but haven't figured out how to do this one either. Am used to defining a list of structures in C++, but am unsure if you can do this in C#. As a simplified example, I...
3
by: Obrecht | last post by:
Hi. I am new to VB .NET and am just starting to do some testing with it. I am trying to define a fixed length array of structures within a structure. I pass this structure to a Win32 Btrieve...
4
by: imme929 | last post by:
I got things working until I tried adding this enum to a structure... Public Enum Keyboard EnglishUS EnglishUK Spanish German Italian French
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.