Global variable-sized arrays

C

Chris Johnson

I've got a program that has two functions, and both need to access an
array. However, the size of the array is not even defined until I'm
partially through the main() function. Here's a quick demonstration of
what I need:

#include <stdio.h>
#include <stdlib.h>

void fubar(int,int);

// This is the array I want to make available to both functions
int equation[count];

int main()
{
int y, count = 0;
while(scanf("%d",&y) != EOF)
count++;

fubar(y,count);

printf("%d\n",equation[0]);
}

void fubar(int y, int count)
{
int i;
for(i = 0; i < count; i++)
equation = y;
}

Effectively what I need is to be able to declare global variables within a
function. Is that possible? Or else is there a workaround?
 
J

Jason Whitehurst

Chris said:
Effectively what I need is to be able to declare global variables
within a function. Is that possible? Or else is there a workaround?

int *equation = NULL;

int main(void) {
..
..
..
equation = malloc(sizeof(int) * count);
..
..
..
}
 
J

Jack Klein

#include said:
int *equation = NULL;

Unnecessary for a pointer with static storage duration, which anything
defined at file scope has. Will be initialized to NULL by default.
int main(void) {
.
.
.
equation = malloc(sizeof(int) * count);

The recommend form by most CLC regulars is:

equation = malloc(sizeof *equation * count);

Saves editing and potential mistakes if it later turns out that the
type of the array needs to be changed to long, or float, or anything
other than int.
 
A

Al Bowers

Chris said:
I've got a program that has two functions, and both need to access an
array. However, the size of the array is not even defined until I'm
partially through the main() function. Here's a quick demonstration of
what I need:

#include <stdio.h>
#include <stdlib.h>

void fubar(int,int);

// This is the array I want to make available to both functions
int equation[count];

int main()
{
int y, count = 0;
while(scanf("%d",&y) != EOF)
count++;

fubar(y,count);

printf("%d\n",equation[0]);
}

void fubar(int y, int count)
{
int i;
for(i = 0; i < count; i++)
equation = y;
}

Effectively what I need is to be able to declare global variables within a
function. Is that possible? Or else is there a workaround?


First of all you do not need a global varialbe to do this. You can
use pointer arguments to modify variables.

Since you are modifying the array length, you can use dynamic
allocations to allocate the size as you need it.
For example, you could prototype the function fubar to:

int fubar(int **y, size_t *count, int value);

where y is a pointer to a pointer to the dynamic allocated array.
count is a pointer to a count of elements
and value is the value you wish to insert in the array.
The Function returns an int that will indicate success or failure
of the allocation.

Here is a working example using function realloc to do the allocations.

#include <stdio.h>
#include <stdlib.h>

int AddArray(int **array,size_t *count,int value);

int main(void)
{
int *equation = NULL, value;
size_t i, count = 0;
puts("Enter integer: (Enter 'q' to quit)");
while(1 == scanf("%d",&value))
if(!AddArray(&equation, &count,value)) break;

for(i = 0; i < count;i++)
printf("equation[%u] = %d\n",i,equation);
free(equation);
return 0;
}

int AddArray(int **array, size_t *count, int value)
{
int *tmp = realloc(*array,((*count)+1)*(sizeof *tmp));
if(tmp)
{
*array = tmp;
(*array)[(*count)++] = value;
return 1;
}
return 0;
}
 
J

Jason Whitehurst

Jack said:
Unnecessary for a pointer with static storage duration, which anything
defined at file scope has. Will be initialized to NULL by default.

Of course it will. It seems he is learning though - I figure best to show
him to just init to NULL. =)

equation = malloc(sizeof *equation * count);

either way.
 
M

Mark A. Odell

Of course it will. It seems he is learning though - I figure best to
show him to just init to NULL. =)



either way.

Either the maintenance nightmare way or the self-documenting,
automatically type correcting way?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top