Two-dimensional array to pass parameters

c language function often need to pass through the two-dimensional array, there are three methods may be implemented as follows:

A method, parameter gives the length of the second dimension.

E.g:

#include <stdio.h>
void func(int n, char  str[ ][5] )
{
 int i;
 for(i = 0; i < n; i++)
  printf("/nstr[%d] = %s/n", i, str[i]);
}

void main()
{
 char* p[3];
 char str[][5] = {"abc","def","ghi"};
 func(3, str);
}

The second method parameter is declared as a pointer to an array.

E.g:

#include <stdio.h>
void func(int n, char  (*str)[5] )
{
 int i;
 for(i = 0; i < n; i++)
  printf("/nstr[%d] = %s/n", i, str[i]);
}

void main()
{
 char* p[3];
 char str[][5] = {"abc","def","ghi"};
 func(3, str);
}

 Three methods parameter declared as a pointer to a pointer.

E.g:

#include <stdio.h>
void func(int n, char **str)
{
 int i;
 for(i = 0; i < n; i++)
  printf("/nstr[%d] = %s/n", i, str[i]);
}
void main()
{
 char* p[3];
 char str[][5] = {"abc","def","ghi"};
 p[0] = &str[0][0];
 p[1] = str[1];
 p[2] = str[2];
    func(3, p);

}

Guess you like

Origin www.cnblogs.com/saulgoodman611/p/11453878.html