Declare an Array

Posted By: oligodar

Declare an Array - 07/26/17 09:22

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
Posted By: jcl

Re: Declare an Array - 07/26/17 14:42

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));.
Posted By: Smon

Re: Declare an Array - 12/20/17 10:04

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!
Posted By: Brax

Re: Declare an Array - 12/20/17 11:18

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.
Posted By: Smon

Re: Declare an Array - 12/20/17 12:27

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 File
SR7dynamic.c  (89 downloads)
Posted By: jcl

Re: Declare an Array - 12/20/17 13:45

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.
Posted By: Smon

Re: Declare an Array - 12/20/17 16:44

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++;

?
Posted By: Smon

Re: Declare an Array - 12/20/17 17:00

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?
Posted By: jcl

Re: Declare an Array - 12/21/17 07:21

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.
© 2024 lite-C Forums