Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array and pointers

Status
Not open for further replies.

junnytony

Programmer
Joined
Oct 19, 2005
Messages
1
Location
US
Hi,

I have a few questions:

1. How do I define a pointer to an array of pointers?
2. How do I define a 1-D array and a 2-D array as part of a struct and then initialize them later with the values passed into a function.
3. How do I malloc for these arrays.

Any help is appreciated!!

 
1.
Code:
type* array[]; /* see sacramental char* argv[] in main() */
2. The specification (request) is not complete. Initialization what: extents or elements (or both)? Let's suppose it's extent(s) initialization. Declare the structure as a container for two pointers (the 1st: to 1D array, the 2nd: to 2D array of rows with knonws size), for example:
Code:
#define COLS	10

typedef int  Row[COLS];
typedef Row* Matrix;
/* It's two pointers container */
typedef struct
{
  int*   arr1d; /* Pointer to 1D array of int */
  Matrix arr2d; /* Pointer to 2D array of int */
                /*  array of int[COLS] arrays */
} DynArr;

/* Initialize the structure, allocate it if pdyn == 0 */
DynArr* InitDynArr(DynArr* pdyn, int n1d, int n2d)
{
  int sz1d;
  int sz2d;
  int i, j;
  sz1d = n1d*sizeof(int);
  sz2d = n2d*sizeof(Row);
  if (!pdyn) /* dynamic allocation */
  {
    pdyn = (DynArr*)malloc(sizeof(DynArr)); /*cast for C++*/
  }
  pdyn->arr1d = (int*)malloc(sz1d);   /* cast for C++ */
  pdyn->arr2d = (Matrix)malloc(sz2d); /* cast for C++ */
  /* Initialization */
  for (i = 0; i < n1d; ++i)
      pdyn->arr1d[i] = 0;
  for (i = 0; i < n2d; ++i)
  for (j = 0; j < COLS; ++j)
      pdyn->arr2d[i][j] = 0;
  /* See (uncomment):
  {
  printf("Row:%u Matrix: %u Matrix*: %u %u %u\n",
	sizeof(Row),sizeof(Matrix),sizeof(pdyn->arr2d[0],sz1d,sz2d);
	
  int*	pi;
  Row*	pp;
  pi = &pdyn->arr2d[0][1];
  pp = pdyn->arr2d;
  printf("%p %p\n",pp,pi);
  }
  */
  return pdyn;
}

/* Free arrays, not a structure */
void ClearDynArr(DynArr* pdyn)
{
  if (pdyn)
  {
     free(pdyn->arr1d);
     pdyn->arr1d = 0;
     free(pdyn->arr2d);
     pdyn->arr2d = 0;
  }
}
/* Free a structure with its contents */
void FreeDynArr(DynArr* pdyn)
{
  if (pdyn)
  {
     ClearDynArr(pdyn);
     free(pdyn);
  }
}

int main()
{
  DynArr* p;
  DynArr  local;
  p = InitDynArr(0,10,10);
  printf("%u\n",sizeof(DynArr));
  InitDynArr(&local,100,100);
  FreeDynArr(p);
  ClearDynArr(&local);
  return 0;
}
You can't declare 2D array with (both) dynamic extents in classic C. You can't place dynamic members in a structure (except the last, but I think it's not a good practice too).

So the C language is not a proper tool for 2D totally dynamic arrays handling (with comfort;). Switch to C++ (or remember unforgettable PL/I;) or be ready to write cumbersome (but sometimes effective;) codes in C...

3. See the snippet above...
 
ArkM - maybe you ought to put this in a FAQ. The 2D/3D array question keeps on popping up in the C and C++ forums.
 
xwb, it's a very important program case, I agree (especially in C).
But FAQ writing is much more responsible task than to write brief notes or to propose a good solution.
Alas, my broken English puts obstacles in this way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top