Hi there,

Im trying to create a simple program that creates an 2D array of numbers and prints them to the screen via a function. I think i have got most of the way however, I cant seem to get the print function to print correctly. It's been a while since i coded in C. Can anyone see where i have slipped up?

#include <stdio.h>
#include <stdlib.h>

#define START        0
#define MAX          128

void printarray(int *,int);

int main(int argc, char *argv[])
{
  int i,j,c;
  int vig[MAX][MAX];
        
      for(i=0;i<MAX;i++)
        {
        c=START+i;            
           for(j=0;j<MAX;j++)
               {
                  if(c<MAX)        /* This section is a simple loop that initiates a Vigenère Table  */
                  {                /* into an array called "vig". This array will be used in the     */               
                  vig[i][j]=c;     /* encryption/decryption process later on.                        */
                  c++;              
                  }
                  else
                  {
                  c=START;   
                  vig[i][j]=c;
                  c++; 
                  }
              }                                         
        }
    
  
  printarray(&vig[0][0], MAX);
       
  system("PAUSE");  
  fclose(input);   	
  return 0;
}

void printarray(int *vig1,int n)
{
 int i,j,x;
 int vig2[MAX][MAX];
 
 vig1=&vig2[MAX][MAX];
       
  for(i=0;i<MAX;i++)                    
  {
        for(j=0;j<MAX;j++)
        {
        printf(" %d,",vig2[i][j]);
        } 
   printf(" \n");                  
  } 
   
}

Also I couldn't remember how to pass the array to the function without having to assign the pointer to a new array :

vig1=&vig2[MAX][MAX];

I didn't think you had to do this but cant get it to pass it in any other way.

Thanks for your time.

Craig

>>void printarray(int *,int);
That is the wrong prototype because one star is a 1d array.

void printarray(int **,int);

or
void printarray(int *array[],int);

or
void printarray(int array[MAX][MAX],int);

My goodness... i feel foolish :)

Many thanks for that.. it jogged my memory

Craig

Ok new question on the same topic,

I'm now trying to make the print function generic so that instead of printing just one 2d array it could print any 2d array passed to it. (hope that makes sense)

I have amended the code and the function looks as follows:

void print2darray(int **vig1, int n)  /* function to print the array . 'n' is the array size */
{
 int i,j;
       
  for(i=0;i<n;i++)                    
  {
        for(j=0;j<n;j++)
        {
        printf(" %d,",vig1[i][j]);
        } 
   printf(" \n");                  
  } 
   
}

the function is defined as: void print2darray(int **,int); and is called as : print2darray(vig,MAX); the compiler does not like the calling line as states that it is an incompatible pointer type.

> void printarray(int **,int);
> void printarray(int *array[],int);
Neither of these are valid for passing a true 2D array.

void foo ( int **p ) {
}
void bar ( int *p[] ) {
}
void baz ( int p[][7] ) { // p[3][7] would also work
}
void qux ( int (*p)[7] ) {
}

int main() {
    int L[3][7];
    foo(L);
    bar(L);
    baz(L);
    qux(L);
    return 0;
}

$ gcc foo.c
foo.c: In function `main':
foo.c:12: warning: passing arg 1 of `foo' from incompatible pointer type
foo.c:13: warning: passing arg 1 of `bar' from incompatible pointer type
This question has already been answered. Start a new discussion instead.