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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2D arrays 1

Status
Not open for further replies.

neonep

Programmer
Jun 26, 2003
34
This is the sample code of what I need to do.

int main ( ... )
{
char array [5] [20];
myfunction ( &array );
}

int myfunction ( char * myarray [] )
{
char * array = myarray;

strcpy ( array[0], "Hello" );
}

is that how you
1. declare a 2 dimensional array, ( char array [5] [20];)
2. pass it to a function, ( myfunction (&array ) )
3. accept it in the function ( int myfunction(char* myarray[])
4. create a pointer to the array, (char * array = myarray )
5. and write to the array? ( strcpy ( array[0], "Hello" ) )

I guess its not because it's not working.
Please help.
 
Code:
void myfunction ( char array[5][20] );

int main ( )
{
 char array[5][20];
 myfunction ( array );
}

int myfunction ( char array[5][20] )
{
  strcpy ( array[0], "Hello" );
}
This is easy to remember, since it's just a copy/paste of the declaration of the array you want to pass to the function.

There are two additional ways you can declare the function.
Code:
void myfunction ( char array[][20] );
void myfunction ( char (*array)[20] );
All three versions mean exactly the same thing.

--
 
how do i do it if the function doesn't know that the array is of size 5 or each index is of size 20 for that matter? like char array [][] or char * array[]
 
Something like this?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>






void printArr (char **, int);

int
main (void)
{
  int i, p;
  char one[10][20];
  char two[15][30];
  char **pt1, **pt2;

  printArr ((pt2 = (char **) &two), 15);
  printArr ((pt1 = (char **) &one), 10);
  return 0;
}

void
printArr (char **arr, int num)
{
  int y;
  for (y = 0; y < num; y++)
    {
      printf ("%p\n", arr++);
    }
}
 
or
allocate and load up your array memory, it gets initialize with NULL, and when you hit NULL means end of the road, then you need no counters. use realloc to expand your 1 dimension. also you need not necessarily allocate 20 bytes, as it can be the size of the line you read.

void printArr (char **);

int
main (void)
{
int i, p;
char **pt1;

ptl = (char **)calloc(1,15);
if (!ptl)
{
error allocating memory
}
// load up the array
for (i=0;i<15;i++)
{

read or whatever 20 bytes
for 0 thru 13 ( 1 less )
ptl=(char *)calloc(1,21);
if (!ptl)
{ memory allocation error }
}

printArr (pt1);
return 0;
}

void printArr (char **arr)
{
int y;
for (y = 0; arr[y] != NULL; y++)
{
printf ("%s\n", arr[y]);
}
}

 
oops memory alloc check needs to be this.

ptl=(char *)calloc(1,21);
if (!ptl)
{ memory allocation error }
 
> how do i do it if the function doesn't know that <...> each index is of size 20 for that matter

Code:
#include <iostream>

void myfunction(int *myarray, int dim_ii, int dim_jj)
{
int ii;
int jj;

   for(ii=0; ii<dim_ii; ii++)
      for(jj=0; jj<dim_jj; jj++)
      {
// accessing element myarray[ii][jj]
         myarray[ii*dim_jj+jj]=ii*1000+jj;
      }
}


int main (void)
{
int array[5][20];
int ii;
int jj;

   myfunction((int *)array, 5, 20);

   for(ii=0; ii<5; ii++)
      for(jj=0; jj<20; jj++)
         cout << array[ii][jj] << " ";

return(0);
}
 
Status
Not open for further replies.

Similar threads

Replies
9
Views
230
Replies
2
Views
147
Replies
1
Views
155

Part and Inventory Search

Sponsor

Back
Top