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]);
}

4
  • 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. Commented Jul 22, 2011 at 15:29
  • @pmg: OTOH, "complicating" the function prototype makes explicit what the function is working with. Commented Jul 22, 2011 at 16:00
  • Yeah @ninja. I have a great proof that global variables are bad, but this comment box is too small for it :) Commented Jul 22, 2011 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. Commented Jul 22, 2011 at 20:14

3 Answers 3

4

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

char * buffer;

int main(void) {

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

@user:606723: This is a safer and more efficient way of dealing with 2d arrays than pointers-to-pointer.
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]);
}

4 Comments

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.
Perhaps, but I think this a good compromise for someone who might not be up for using 'appropriate indexing tricks'.
I've tried to comment but I can't make new lines in comments so I edited my main post. thanks!
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!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.