3

I need to declare a global two-dimensional array in C.

The size of the array is determined by the width and height of a given picture.

So I first have to load the picture, and only then create the array. But if I want a variable (in this case, my array) to be global, I have to declare it at the top of the file and not inside a function.

So how can I declare a array as global when I only know its size after the execution of the main() function?

EDIT: (I've also tried the other solutions so this comments refers to all of them)@Mimisbrunnr First, thanks for the quick response!

I've tried but I can't see to make it work. I'm probably missing something stupid, but how does "array" becomes global? It says on test() that 'array' is undeclared

int *buffer;

int main() {
    int i;
    int x_size=100;
    int y_size=100;

    int * buffer = malloc(sizeof(int)*x_size*y_size);
    int ** array = malloc(sizeof(int*)*y_size);

    for(i = 0; i<y_size; i++) {
        array[i]=&buffer[i*x_size];
    }

    array[0][1] = 5;
    test();
    return 0;
}

void test(){
    printf("%d",array[0][1]);
}

zellio
  • 31,308
  • 1
  • 42
  • 61
Avenger
  • 441
  • 1
  • 3
  • 11
  • 4
    Do you really need a global variable (pointer)? Can't you pass it around instead? Passing it around "complicates" the function prototypes but makes management, especially when dealing with multi-threaded applications, easier. – pmg Jul 22 '11 at 15:29
  • @pmg: OTOH, "complicating" the function prototype makes explicit what the function is working with. – ninjalj Jul 22 '11 at 16:00
  • Yeah @ninja. I have a great proof that global variables are bad, but this comment box is too small for it :) – pmg Jul 22 '11 at 16:03
  • I could pass it... but as you said it will really complicate my code. And since I'm now just doing it to learn C, I'd want to know all the options on how to solve this problem so later when I'll confront it in a real-life program I'd be able to do it in the best way possible. – Avenger Jul 22 '11 at 20:14

3 Answers3

4

create a global pointer and then malloc the space into it.

char * buffer;

int main(void) {

    buffer = malloc( /* Width * Height */ );

}
zellio
  • 31,308
  • 1
  • 42
  • 61
  • 1
    @user:606723: This is a safer and more efficient way of dealing with 2d arrays than pointers-to-pointer. – Fred Foo Jul 22 '11 at 15:35
1

I didn't actual execute this code, but this should get you started.

int x_size = 100;
int y_size = 100;

int ** array;
array = malloc(sizeof(int *)*y_size);
for(int i = 0; i<y_size; i++)
array[i] = malloc(sizeof(int)*x_size);

larsmans made a good point.

what about this?

int x_size = 100;
int y_size = 100;

int * buffer = malloc(sizeof(int)*x_size*y_size);

int ** array = malloc(sizeof(int *)*y_size);
for(int i = 0; i<y_size; i++)
array[i] = &buffer[i*x_size];

It looks like you might need some basic C tutorial.

int *buffer;
int **array;
int main()
{
int x_size=100;
int y_size=100;
int i;
/*int * */ buffer = malloc(sizeof(int)*x_size*y_size);
/*int ** */ array = malloc(sizeof(int*)*y_size);
for(i = 0; i<y_size; i++)
 array[i]=&buffer[i*x_size];
array[0][1] = 5;
test();
return 0;
}
void test()
{
printf("%d",array[0][1]);
}
user606723
  • 4,918
  • 2
  • 27
  • 34
  • This is slightly more compact, but you still need two calls to `free` to get rid of the data (I know, it's global...). Also, it's still less memory-efficient than using a single array with the appropriate indexing tricks, and might suffer from loss of locality of reference. – Fred Foo Jul 22 '11 at 15:45
  • Perhaps, but I think this a good compromise for someone who might not be up for using 'appropriate indexing tricks'. – user606723 Jul 22 '11 at 15:53
  • I've tried to comment but I can't make new lines in comments so I edited my main post. thanks! – Avenger Jul 22 '11 at 20:07
  • Thanks! It works perfectly well! I'm now learning C, the whole pointer thingies are very complicated for me - all the *, **, $, malloc and etc. Anyway, Thank you very much for the comments! though I'm not sure I understand it, I'll try to re-read the variables & pointers chapter in my book! – Avenger Jul 22 '11 at 20:59
0

use a static variable (pointer) and allocate the array dynamically using malloc.

eyalm
  • 3,366
  • 19
  • 21