I have created a 2D array in my main function and want to update it in another function . As per my first thought I need to declare the Array dynamically , but the problem is that the dimension should also be dynamic -
following is what I am trying -
int l;
int m;
int n;
void *Hello(void* rank); // Prototype of a thread function
int main(int argc,char* argv[])
{
int l = atoi(argv[2]);
int m = atoi(argv[3]);
int n = atoi(argv[4]);
int A[l][m];
pthread_create(&myThreads[thread_id], NULL,Hello, (void*)thread_id);
}
void *Hello(void* rank)
{ }
I want to update Array A from *Hello function , please advice me for right implementation.
First, you should allocate the memory for the 2D array in the heap:
note that the array isn't in contiguous region of memory.
then you need pass the
A
to theHello()
using the fourth parameter like this:then in the function
Hello()
, cast thevoid*
back toint**
after that, you can update Array
A
fromHello()
function.Here is an example without any error checking: