Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (MadJack, AndrewAMD, Quad), 540 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Declare an Array #467319
07/26/17 09:22
07/26/17 09:22
Joined: Dec 2013
Posts: 14
O
oligodar Offline OP
Newbie
oligodar  Offline OP
Newbie
O

Joined: Dec 2013
Posts: 14
Code:
void main(){
	int howManyItems = 10;
	int myArray[howManyItems];

	return;
}



Hi, compiler complains about an error in line 4.
Whats the problem? I am not allowed to declare an Array this way?

Thank you a lot

Last edited by oligodar; 07/26/17 09:39.
Re: Declare an Array [Re: oligodar] #467327
07/26/17 14:42
07/26/17 14:42
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
A static array size cannot be a variable. If you don't know beforehand the size of your array, you must allocate a dynamic array, like this:

int *myArray = (int*)malloc(howManyItems*sizeof(int));.

Re: Declare an Array [Re: jcl] #470033
12/20/17 10:04
12/20/17 10:04
Joined: Dec 2014
Posts: 206
Germany
Smon Offline
Member
Smon  Offline
Member

Joined: Dec 2014
Posts: 206
Germany
I tried to implement this:

mystruct *myArray = (mystruct*)malloc(howManyItems*sizeof(mystruct));

When I'm trying to manipulate an item of the array with

*myArray[index].structmember = somevalue


the compiler tells me

"illegal indirection"


What am I doing wrong? An example of how to delete an item at a certain index would be cool, too!

Re: Declare an Array [Re: Smon] #470035
12/20/17 11:18
12/20/17 11:18
Joined: Aug 2017
Posts: 102
Spain
B
Brax Offline
Member
Brax  Offline
Member
B

Joined: Aug 2017
Posts: 102
Spain
Just remove the initial '*':

myArray[index].structmember = somevalue;

If you want to 'quickdelete' items in an array use a struct member for valid items and assign 0 to them, fi:

myArray[index].activeitem = 0;

If you want to be more precise and memory efficient, then you'll have to create a standard pointer list. That is, you'll have to add a next element struct member and whenever you delete an item, iterate through the list and assign the next element members.

Use the solution you like the most.

Last edited by brax; 12/20/17 11:22.
Re: Declare an Array [Re: Brax] #470036
12/20/17 12:27
12/20/17 12:27
Joined: Dec 2014
Posts: 206
Germany
Smon Offline
Member
Smon  Offline
Member

Joined: Dec 2014
Posts: 206
Germany
Thanks @brax.

I already noticed, that the compiler doesn't give me this error when I remove the "*", but then it's even worse:

Error 111: Crash in script: run()

No further explanation from the compiler is given. I attached the *.h file and would be glad if somebody could take a look. It's an early version of a support and resistance indicator.

The standard pointer list sounds complicated, but thanks for the hint. I thought I had to use the "free" function, like free(*void, myArray[index]).... Is this possible, too?

Attached Files
SR7dynamic.c (88 downloads)
Re: Declare an Array [Re: Smon] #470039
12/20/17 13:45
12/20/17 13:45
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
Compilers can only find syntax errors, finding bugs is the programmer's job. First, try what's described in the manual under "troubleshooting". Second, if you could not find the bug this way, ask for help here.

Re: Declare an Array [Re: jcl] #470049
12/20/17 16:44
12/20/17 16:44
Joined: Dec 2014
Posts: 206
Germany
Smon Offline
Member
Smon  Offline
Member

Joined: Dec 2014
Posts: 206
Germany
I narrowed the error down.

It seems to have something to do with the static declaration I'm using. I didn't think this is important:

static mystruct *myArray = (mystruct*)malloc(howManyItems*sizeof(mystruct));

If I remove "static", the script runs fine apart from that it's now storing ridiculous numbers in the array and I don't know where these numbers are coming from.

Is the array dynamically expanded by doing

howManyItems++;

?

Last edited by sdh309795gaas; 12/20/17 16:46.
Re: Declare an Array [Re: Smon] #470051
12/20/17 17:00
12/20/17 17:00
Joined: Dec 2014
Posts: 206
Germany
Smon Offline
Member
Smon  Offline
Member

Joined: Dec 2014
Posts: 206
Germany
Okay okay, I was quite naive about dynamic arrays in C. I noticed that after a little looking around on stackoverflow.com

Seems like I must either master pointers or stick with a static array.

The whole point of making the array dynamic was that I'm fearing to run out of stack memory further down the road. Is it sufficient to declare the array as a pointer and make it rather big to prevent this?

Last edited by sdh309795gaas; 12/20/17 17:01.
Re: Declare an Array [Re: Smon] #470064
12/21/17 07:21
12/21/17 07:21
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
A static global oversized array is normally the simplest solution.

Only when that array would be hundreds of megabytes, it makes sense to use pointers and malloc for not wasting memory.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1