【问题标题】:c language array problemsc语言数组问题
【发布时间】:2011-05-02 07:41:30
【问题描述】:

嗨 我是 c 语言的新手,我有一个问题: 我想通过指针将二维数组发送到函数。 该函数应返回指向二维数组的指针。 我为此编写了以下代码:

#include<stdio.h>
int* put(int *b);
int main()
{
  int a[2][3],i,j;
  system("clear");  
  put(a);

  for(i=0;i<2;i++)
    { 
      for(j=0;j<3;j++)
        {    
          printf("\na[%d][%d]= %d",i,j,a[i][j]);
        }
    }

  return 0;
}

int* put(int *b)
{
  for(i=0;i<2;i++)
  {
    for(j=0;j<3;j++)
      {
        b[i][j]=i;
      }
  }
  return b;
}

当我用 gcc2de.c 编译它时,它显示以下错误:

2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int *’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:28: error: subscripted value is neither array nor pointer
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input

我只是更改以下函数的代码:

#include<stdio.h>

int* put(int **b);

int main()
{
  int a[2][3],i,j;
  system("clear");  
  put(a);

  for(i=0;i<2;i++)
    { 
      for(j=0;j<3;j++)
        {    
          printf("\na[%d][%d]= %d",i,j,a[i][j]);
        }
    }

  return 0;
}

int* put(int **b)
{
  for(i=0;i<2;i++)
    {
      for(j=0;j<3;j++)
        {
          b[i][j]=i;
        }
    }
  return b;
}

当我编译它时,我得到了以下错误:

2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input
2de.c: In function ‘main’:
2de.c:9: warning: passing argument 1 of ‘put’ from incompatible pointer type
2de.c:3: note: expected ‘int **’ but argument is of type ‘int (*)[3]’
2de.c: In function ‘put’:
2de.c:31: warning: return from incompatible pointer type
2de.c: In function ‘main’:
2de.c:32: error: expected declaration or statement at end of input

我做错了什么? 谁能告诉我通过指向函数的指针传递二维数组的方法是什么? 谁能告诉我如何通过函数中的指针返回两个 d 数组

【问题讨论】:

  • 您在printf("\na[%d][%d]= %d",i,j,a[i][j]); 之后错过了},这导致错误阻止编译。

标签: c arrays pointers


【解决方案1】:

您遇到的第一个错误是您没有传递函数声明的正确类型。因此,要以最少的更正来清理您的代码,它可能看起来像这样:

#include<stdio.h>

void put(int *b);

int main()
{
  int a[2][3],i,j;

    put(&a[0][0]);

  for(i=0;i<2;i++)
  { 
    for(j=0;j<3;j++)
    {    
      printf("\na[%d][%d]= %d", i, j, a[i][j]);
    }
  }

  printf("\n\n");

  system("PAUSE");  // Not recommended, but works for now
return 0;
}

void put(int *b)
{
  int count = 1;
  int i, j;

  for(i=0;i<2;i++)
  {
    for(j=0;j<3;j++)
    {
      //b[i][j]=i;
      *(b + ((i*3) + j)) = count++;
    }
  }

}

两个主要的更正是:

  1. 您将二维数组的起始地址显式传递为 &a[0][0]。
  2. 另外,请注意在使用 int *b 时必须使用的指针算法。

还请注意,由于您传递的是指针,因此您正在修改该地址位置的值。因此根本不需要返回一个指针。

希望对您有所帮助。干杯!

【讨论】:

    【解决方案2】:

    你将put的返回值存储在哪里?

    根据你的代码,声明应该是int** put( int **)

    【讨论】:

      【解决方案3】:

      您遇到的第一个错误是您试图在另一个函数中定义一个函数。最简单的做法是在声明它的位置定义 put

      int put()
      {
          /* definition of put */
      }
      
      int main()
      {
          /* body calls put */
      }
      

      第二个问题是,在这两个代码 sn-p 中,您都没有将兼容的参数传递给put

      如果您想将a 传递给函数,那么您应该注意,作为参数的数组总是衰减为指向其第一个元素的指针。

      a 的类型为int [2][3],即由 2 个数组组成的数组,每个数组包含 3 个ints。这将衰减为指向 3 个 ints 或 int (*)[3] 的数组的指针。这应该解释你得到的编译错误。您应该将put 声明为:

      void put(int (*b)[3]);
      

      或完全等价的:

      void put(int b[][3]);
      

      由于不能按值传递数组,编译器会自动将采用数组参数的函数声明转换为采用等效指针参数的函数声明。

      我已将返回类型更改为void,因为您在通过指针传递参数时不需要或不需要返回值。您应该从 put 的定义中删除 return b;

      提示:不要将int[2][3] 视为一个二维数组,而是一个数组数组。

      【讨论】:

        【解决方案4】:

        不能从 C 中的函数返回二维数组,只能返回指向二维数组第一个元素的指针的一维数组。

        也许你会发现这个有用:

        Pass 2d array to function in C?

        或者这个:

        C++ Returning multidimension array from function

        【讨论】:

          【解决方案5】:

          1.函数使用前应先声明或定义,与其他流行语言不同。

          2.put函数中不需要返回指针,数组中的数据已经改变了

          3.需要注意类型,int array[][]的类型是int **

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-07-14
            • 1970-01-01
            • 2016-01-21
            • 1970-01-01
            • 2021-07-20
            • 2021-12-16
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多