hi folks,
i thought i should share something with people who find it difficult to work with 2D arrays using functions. Notice the following :
#include<stdio.h>
void dosomething(char (*virtualarray) [11])
{
...
/* anything you want to do to virtualarray */
...
return;
}
main()
{
char mainarray[11][11];
...
dosomething(mainarray);
...
return 0;
}
anything you do to virtualarray in dosomething gets reflected in mainarray in function main().
In dosomething, you can have random access by
virtualarray[i] [j] which points to the jth element of ith row
Alternatively, you can use (*virtualarray) [j] pointing to the jth element of whichever row virtualarray is initialised to (which is the first row when you enter dosomething). you can increment virtualarray to point to the next row if you need sequential access. To beginners, i would say... use the previous method incrementing 'i' each time to move on to the next row. Using the second method can really bug you if not done properly. I myself ran into trouble trying it out
Actually i m still in the process of figuring out how this works cause i saw it in a solved example in a book. Suggestions please ...
Regards,
Goran.