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

Home Posts Topics Members FAQ

Defining/dealing with a dynamical structure (C)

2 New Member
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_simu lation.c: In function ‘Determine_Even t’:
stochastic_simu lation.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 1148
weaknessforcats
9,208 Recognized Expert Moderator Expert
possible_events =(EVENT *) realloc(possibl e_events,(nb_ev ent+1)*sizeof(E VENT) );
if (possible_event s == 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 New Member
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
1232
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 two tags and all the content between them? Thank you very much. -- Regards,
0
1108
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
1138
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 looking for. i am looking for code to make a dynamical menu with submenus The menu items are stored in a MS SQL Table and when by loading the page the items will be created. i have made a asp table. i want my menu on the left cell, in the middle...
4
3530
by: FraterQ | last post by:
Hello, after i've read this post, i tried it myself and it works. http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=utf-8&threadm=9jjh9m%247bb%242%40oravannahka.helsinki.fi&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3Dutf-8%26q%3Dresize%2Barray%2Bc%2Brealloc%26btnG%3DGoogle%2BSearch But I've the following problem: I can't determine the size of the dynamical generated array through a predefined function like sizeof(). Can...
5
38363
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 have: struct Command { int commandEnum; string commandName; int commandLength; byte command;
3
5171
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 API. I have one main structure and within that I need to have a fixed length array of 200 other structures. Below - shortened versions of the structures.
4
1809
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
2117
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 multiple arrays,etc) I do not understand how to define the structure at run time.i.e.what fields it will contain.
0
2185
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 problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
8438
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
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 we have to send another system
2
1761
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.