I am learning C and am having trouble passing the pointer of a 2D array to another function that then prints the 2D array. Any help would be appreciated.
我正在学习C并且无法将2D数组的指针传递给另一个函数,然后打印出2D数组。任何帮助,将不胜感激。
int main( void ){
char array[50][50];
int SIZE;
...call function to fill array... this part works.
printarray( array, SIZE );
}
void printarray( char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
4 个解决方案
#1
22
char **
doesn't represent a 2D array - it would be an array of pointers to pointers. You need to change the definition of printarray
if you want to pass it a 2D array:
char **不代表2D数组 - 它是指向指针的数组。如果要将其传递给2D数组,则需要更改printarray的定义:
void printarray( char (*array)[50], int SIZE )
or equivalently:
或等效地:
void printarray( char array[][50], int SIZE )
#2
5
In main(), the variable "array" is declared as
在main()中,变量“array”声明为
char array[50][50];
This is a 2500 byte piece of data. When main()'s "array" is passed about, it is a pointer to the beginning of that data. It is a pointer to a char expected to be organized in rows of 50.
这是一个2500字节的数据。传递main()的“数组”时,它是指向该数据开头的指针。它是指向预期以50行组织的char的指针。
Yet in function printarray(), you declare
然而在函数printarray()中,您声明了
char **array
"array" here is a pointer to a "char *pointer".
“array”这里是指向“char *指针”的指针。
@Lucus suggestion of "void printarray( char array[][50], int SIZE )" works, except that it is not generic in that your SIZE parameter must be 50.
@Lucus建议“void printarray(char array [] [50],int SIZE)”工作,但它不是通用的,因为你的SIZE参数必须是50。
Idea: defeat (yeech) the type of parameter array in printarray()
想法:打败(yeech)printarray()中参数数组的类型
void printarray(void *array, int SIZE ){
int i;
int j;
char *charArray = (char *) array;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", charArray[j*SIZE + i] );
}
printf( "\n" );
}
}
A more elegant solution is to make the "array" in main() an array of pointers.
更优雅的解决方案是使main()中的“数组”成为指针数组。
// Your original printarray()
void printarray(char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
// main()
char **array;
int SIZE;
// Initialization of SIZE is not shown, but let's assume SIZE = 50;
// Allocate table
array = (char **) malloc(SIZE * sizeof(char*));
// Note: alternative syntax
// array = (char **) malloc(SIZE * sizeof(*array));
// Allocate rows
for (int row = 0; row<SIZE; row++) {
// Note: sizeof(char) is 1. (@Carl Norum)
// Shown here to help show difference between this malloc() and the above one.
array[row] = (char *) malloc(SIZE * sizeof(char));
// Note: alternative syntax
// array[row] = (char *) malloc(SIZE * sizeof(**array));
}
// Initialize each element.
for (int row = 0; row<SIZE; row++) {
for (int col = 0; col<SIZE; col++) {
array[row][col] = 'a'; // or whatever value you want
}
}
// Print it
printarray(array, SIZE);
...
#3
0
Since C99 supports dynamic-sized arrays, the following style is simply more convenient to pass a 2-dim array:
由于C99支持动态大小的数组,因此以下样式传递2-dim数组更方便:
void printarray( void *array0, int SIZE ){
char (*array)[SIZE] = array0;
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
#4
0
You can easily pass the 2d array using double pointer.
您可以使用双指针轻松传递2d数组。
void printarray( char **array, int n)
{
int i, j;
for(i=0; i<n; i++ )
{
for(j=0; j<n; j++)
{
printf("%c ", array[i][j] );
}
printf( "\n" );
}
}
int main()
{
int n = 2;
int i, j;
char **array = (char **) malloc(n * sizeof(char*));
for (i=0; i<n; i++)
{
array[i] = (char *) malloc(n* sizeof(char));
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
scanf("%c ", &array[i][j]);
}
}
printarray(array, n);
return 0;
}
Full Code : Ideone
完整代码:Ideone
#1
22
char **
doesn't represent a 2D array - it would be an array of pointers to pointers. You need to change the definition of printarray
if you want to pass it a 2D array:
char **不代表2D数组 - 它是指向指针的数组。如果要将其传递给2D数组,则需要更改printarray的定义:
void printarray( char (*array)[50], int SIZE )
or equivalently:
或等效地:
void printarray( char array[][50], int SIZE )
#2
5
In main(), the variable "array" is declared as
在main()中,变量“array”声明为
char array[50][50];
This is a 2500 byte piece of data. When main()'s "array" is passed about, it is a pointer to the beginning of that data. It is a pointer to a char expected to be organized in rows of 50.
这是一个2500字节的数据。传递main()的“数组”时,它是指向该数据开头的指针。它是指向预期以50行组织的char的指针。
Yet in function printarray(), you declare
然而在函数printarray()中,您声明了
char **array
"array" here is a pointer to a "char *pointer".
“array”这里是指向“char *指针”的指针。
@Lucus suggestion of "void printarray( char array[][50], int SIZE )" works, except that it is not generic in that your SIZE parameter must be 50.
@Lucus建议“void printarray(char array [] [50],int SIZE)”工作,但它不是通用的,因为你的SIZE参数必须是50。
Idea: defeat (yeech) the type of parameter array in printarray()
想法:打败(yeech)printarray()中参数数组的类型
void printarray(void *array, int SIZE ){
int i;
int j;
char *charArray = (char *) array;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", charArray[j*SIZE + i] );
}
printf( "\n" );
}
}
A more elegant solution is to make the "array" in main() an array of pointers.
更优雅的解决方案是使main()中的“数组”成为指针数组。
// Your original printarray()
void printarray(char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
// main()
char **array;
int SIZE;
// Initialization of SIZE is not shown, but let's assume SIZE = 50;
// Allocate table
array = (char **) malloc(SIZE * sizeof(char*));
// Note: alternative syntax
// array = (char **) malloc(SIZE * sizeof(*array));
// Allocate rows
for (int row = 0; row<SIZE; row++) {
// Note: sizeof(char) is 1. (@Carl Norum)
// Shown here to help show difference between this malloc() and the above one.
array[row] = (char *) malloc(SIZE * sizeof(char));
// Note: alternative syntax
// array[row] = (char *) malloc(SIZE * sizeof(**array));
}
// Initialize each element.
for (int row = 0; row<SIZE; row++) {
for (int col = 0; col<SIZE; col++) {
array[row][col] = 'a'; // or whatever value you want
}
}
// Print it
printarray(array, SIZE);
...
#3
0
Since C99 supports dynamic-sized arrays, the following style is simply more convenient to pass a 2-dim array:
由于C99支持动态大小的数组,因此以下样式传递2-dim数组更方便:
void printarray( void *array0, int SIZE ){
char (*array)[SIZE] = array0;
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
#4
0
You can easily pass the 2d array using double pointer.
您可以使用双指针轻松传递2d数组。
void printarray( char **array, int n)
{
int i, j;
for(i=0; i<n; i++ )
{
for(j=0; j<n; j++)
{
printf("%c ", array[i][j] );
}
printf( "\n" );
}
}
int main()
{
int n = 2;
int i, j;
char **array = (char **) malloc(n * sizeof(char*));
for (i=0; i<n; i++)
{
array[i] = (char *) malloc(n* sizeof(char));
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
scanf("%c ", &array[i][j]);
}
}
printarray(array, n);
return 0;
}
Full Code : Ideone
完整代码:Ideone