At 4/6/2007 10:32 AM, you wrote:

>--- Rick <[EMAIL PROTECTED]> wrote:
>
> > I have a program I'm trying to write and can't
> > figure out how to do
> > it the way I want.
> >
><<SNIP>>
>
>In a two dimensional array the compiler needs to know
>now many columns there are in order to calculate the
>address of the elements at he beginning of each row.
>
>
>You can do something like this :
>
>#define ROW 10
>#define COL 5
>
>void somefunct(char data[ROW][COL]);
>
>or
>
>void somefunct(char data[][COL], int NumOfRows);
>
>but this
>
>void somefunct(char data[][]);
>
>will not work in C.
>
>
>You would think there would be a way to pass the
>arrays dimension but C has no such feature.
>
>Again when passing two dimensional array to function
>in C you must at least specify the number of columns.
>
>Mickey M.
>Construction Partner Inc.
>http://www.constructionpartner.com

Thanks! That is exactly what I needed to do. (Thanks, also, Nico).

Here is what works:
#include <stdio.h>
#include <stdlib.h>

#define  COLCNT   32
#define  ROWCNT   256

extern   void loadcArray(char [][COLCNT], int);
extern   void printcArray(char [][COLCNT], int);

int main(int argc, char *argv[])
{
     int result;
     char array2[5][COLCNT] = {"string1", "String2", "String3", 
"string4", "string5"};
     int a2size = (sizeof(array2) / sizeof(array2[0]));

     loadcArray(array2, a2size);
     printcArray(array2, a2size);

     system("PAUSE");
     return 0;
}


void loadcArray(char cArray[][COLCNT], int alen)
{
          int ix;
          char val[10240];

          for (ix = 0; ix < alen; ix++)
          {
              printf("Enter a string value for array[%d/%d]: ", ix, alen);
              fscanf(stdin, "%s", val);
              if (strlen(val) >= COLCNT)
                 val[COLCNT-1] = '\0';
              strcpy(cArray[ix], val);
          }
          printf("Finished loading %d values\n", ix);
          return;
}


void printcArray(char cArray[][COLCNT], int alen)
{
      int ix;

      printf("\n");
      for (ix = 0; ix < alen; ix++)
      {
          printf("Array[%d] = %s\n", ix, cArray[ix]);
      }
      return;
}

Reply via email to