2D array in file/program scope

2019-09-16 05:34发布

I need an array which I can access from different methods, I have to allocate this array in main() and then let other functions like foo() get access to this array.

This question helped me with allocating the array: defining a 2D array with malloc and modifying it
I'm defining the array like this: char(*array)[100] = malloc((sizeof *array) * 25200); And I'm doing this in main()
I can store 25200 strings in this array an access them by array[1]

Is it now possible to access this array from different methods, how can I do that?

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-16 06:24

With this declaration:

char (*array)[100] = malloc((sizeof *array) * 25200);

You can have a function foo:

void foo(char array[][100])
{
    array[42][31] = 'A';  // you can access characters elements this way
    strcpy(array[10], "Hello world\n");  // you can copy a string this way
}

and you can call foo this way:

foo(array);
查看更多
登录 后发表回答