C Question - Global Arrays

Soldato
Joined
22 Oct 2005
Posts
2,883
Location
Moving...
I want to define an array globally, but I don't know the size of the array initially. I have a function that calculates the required array size in the program, I want to set the size of it after I have executed this function.

How can you define an array globally without specifying it's size at the same time? Is this is even possible? If not, what else can I do apart from creating the array locally and passing it to every function that needs it?

(I'm using straight C btw)

Thanks for any help.
 
Thanks again voodooflux. Works perfectlly.

As you may have guessed I'm a bit of a novice, would you mind explaining how this works briefly?

I understand that malloc is used basically to allocate a certain amount of space, the space it creates is based on what's in the brackets. That I understand.

The thing I dont understand is how you've defined a pointer (i think) with the code char* myArray, and basically created an array from that. The only difference I can see is that when I go to debug it, I can only view one element. It still seems to work in exactly the same way as an array:confused:
 
Back
Top Bottom