Velocity Reviews > how to define global variable in main()

# how to define global variable in main()

Davor
Guest
Posts: n/a

 07-02-2003

How to define global variable in main()?
I'm asking because I have an array in main, whose size is determined by
input, so the definition has to be in main ( or in some other funcion ).
And I need to use that array in my other functions, so I want it to be
global. I tryed using extern keyword, but I gut some error, so I supose
that's not it.

Davor

Jeff
Guest
Posts: n/a

 07-02-2003
Davor wrote:
>
>
> How to define global variable in main()?
> I'm asking because I have an array in main, whose size is determined by
> input, so the definition has to be in main ( or in some other funcion ).
> And I need to use that array in my other functions, so I want it to be
> global. I tryed using extern keyword, but I gut some error, so I supose
> that's not it.
>
>
> Davor
>

Is there a reason why you don't just past that array to every function
that needs to access it?

Jeff
Guest
Posts: n/a

 07-02-2003
Jeff wrote:
> Davor wrote:
>
>>
>>
>> How to define global variable in main()?
>> I'm asking because I have an array in main, whose size is determined
>> by input, so the definition has to be in main ( or in some other
>> funcion ). And I need to use that array in my other functions, so I
>> want it to be global. I tryed using extern keyword, but I gut some
>> error, so I supose that's not it.
>>
>>

>
> Is there a reason why you don't just past that array to every function
> that needs to access it?
>

s/past/pass/g

Ugh!

Neil Cerutti
Guest
Posts: n/a

 07-02-2003
In article <(E-Mail Removed)>, Davor wrote:
> How to define global variable in main()? I'm asking because I
> have an array in main, whose size is determined by input, so
> the definition has to be in main ( or in some other funcion ).
> And I need to use that array in my other functions, so I want
> it to be global. I tryed using extern keyword, but I gut some
> error, so I supose that's not it.

#include <stdlib.h>

int *global_array;

int get_user_input(void);
void go(void);

int main(void)
{
int x = get_user_input();
global_array = malloc(x * *global_array);
if (global_array != 0) {
go();
}
return 0;
}
/* etc... */

--
Neil Cerutti

Richard Heathfield
Guest
Posts: n/a

 07-02-2003
Neil Cerutti wrote:

<snip>

> global_array = malloc(x * *global_array);

ITYM

global_array = malloc(x * sizeof *global_array);

<snip>

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Martin Ambuhl
Guest
Posts: n/a

 07-02-2003
Davor <(E-Mail Removed)> wrote (02 Jul 2003) in
news:(E-Mail Removed) / comp.lang.c:

>
>
> How to define global variable in main()?

main() is a function. Any variable declared in main will have block
scope. Variables with file scope and external linkage should be
declared *outside* of any function.

> I'm asking because I have an array in main, whose size is determined
> by input, so the definition has to be in main ( or in some other
> funcion ).

Not true. A pointer can be declared outside a function, as can a
variable holding size information. The allocation occurs in a function,
but so what?

> And I need to use that array in my other functions, so I want it to be
> global.

If you have a good reason to avoid passing the array name as an
argument, then declare it as file scope. Try to avoid the word
"global", since it could have several meanings. Use the words for which
there is a clear meaning in C (scope, linkage, duration).

--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America

Neil Cerutti
Guest
Posts: n/a

 07-02-2003
In article <bdv50m$jve$(E-Mail Removed)>, Richard Heathfield wrote:
> Neil Cerutti wrote:
>
><snip>
>
>> global_array = malloc(x * *global_array);

>
> ITYM
>
> global_array = malloc(x * sizeof *global_array);

Oops!

--
Neil Cerutti

hercules
Guest
Posts: n/a

 07-03-2003
The size of an array must be determined at compliled session!
it can't be determined at running session!

"Davor" <(E-Mail Removed)> ??????:(E-Mail Removed)...
>
>
> How to define global variable in main()?
> I'm asking because I have an array in main, whose size is determined by
> input, so the definition has to be in main ( or in some other funcion ).
> And I need to use that array in my other functions, so I want it to be
> global. I tryed using extern keyword, but I gut some error, so I supose
> that's not it.
>
>
> Davor
>

Morris Dovey
Guest
Posts: n/a

 07-03-2003
hercules wrote:
> The size of an array must be determined at compliled session!
> it can't be determined at running session!

Hercules...

Not true. See http://www.iedu.com/mrd/c/tokenize.c for an
example; then see http://www.iedu.com/mrd/c/tokfile.c for an
example (that uses the code from the first example) that
dynamically produces an array of pointers to dynamically produced
arrays.

Neither of these pieces of code know the size of the arrays being
produced until discovering that the final element has been
processed - at which time the memory for the entire array is
allocated and the element values stored.

Both sources contain a short test program with which you're
welcome to play to convince yourself that it really does work as
I describe.
--
Morris Dovey
West Des Moines, Iowa USA

Morris Dovey
Guest
Posts: n/a

 07-03-2003
Martien Verbruggen wrote:
> On Wed, 02 Jul 2003 21:09:25 -0500,
> Morris Dovey <(E-Mail Removed)> wrote:
>
>>hercules wrote:
>>
>>>The size of an array must be determined at compliled session!
>>>it can't be determined at running session!

>>
>>Hercules...
>>
>>Not true. See http://www.iedu.com/mrd/c/tokenize.c for an

>
>
> \begin{pedantry}
>
> There are no arrays in that program. There are pointers and
> allocations with malloc, but that doesn't make an array.
>
> \end{pedantry}
>
> I suspect that hercules was talking about real arrays, and in c89 the
> size of those does need to be known at compile time.

(more pedantic

C99: 7.20.3.1

"The order and contiguity of storage allocated by successive
calls to the calloc, malloc, and realloc functions is
unspecified. The pointer returned if the allocation succeeds is
suitably aligned so that it may be assigned to a pointer to any
type of object and then used to access such an object or an array
of such objects in the space allocated (until the space is
explicitly deallocated)." [remainder of paragraph dropped]

(less pedantic)

Looks like an array to me.
--
Morris Dovey
West Des Moines, Iowa USA