c语言数组问题

发布于 2024-11-05 00:17:07 字数 2156 浏览 0 评论 0原文

你好 我是 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 数组

Hi
I'm new to c language i hava a problem :
i want to send a 2-d array to a function via pointer.
The function should return pointer to 2-d array.
I wrote the following code for this :

#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;
}

when i compile it with gcc2de.c it shows following errors :

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

Than i just change the code of function which is following :

#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;
}

when i complie it i got following errors:

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

what I'm doing wrong ?
can anybody tell me what is the way to pass 2d-array via pointers to a function ?
can anybody tell me how to return two d array via pointer in a function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

靑春怀旧 2024-11-12 00:17:07

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

#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 时也必须使用的指针算术。

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

希望有帮助。干杯!

The first error that you have is that you are not passing a correct type as declared by your function. So to clean up your code with the least amount of corrections, it would probably look something like this:

#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++;
    }
  }

}

The two major corrections are:

  1. You pass in the start address of your 2-D array explicitly by addressing it as &a[0][0].
  2. Also, note the pointer arithmetic that you'll have to use when you use an int *b as well.

Note also that since you're passing in a pointer, you're modifying the value at that address location. Thus there is no need to return a pointer back at all.

Hope it helps. Cheers!

红墙和绿瓦 2024-11-12 00:17:07

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

根据您的代码,声明应为 int** put( int **)

where are you storing the return value from put ?

the declaration should be int** put( int **) according yo your code.

素罗衫 2024-11-12 00:17:07

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

int put()
{
    /* definition of put */
}

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

第二个问题是,在这两个代码片段中都没有向 put 传递兼容的参数。

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

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

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

或完全等价的:

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

因为您无法按值传递数组,所以编译器会自动将采用数组参数的函数声明转换为采用等效指针参数的函数声明。

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

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

The first error you have is that you are trying to define a function inside another function. The simplest thing to do is to just define put where you declare it:

int put()
{
    /* definition of put */
}

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

The second problem is that in neither code snippet are you passing a compatible parameter to put.

If you want to pass a to a function then you should note that arrays as arguments always decay to a pointer to their first element.

a has type int [2][3], i.e. an array of 2 arrays of 3 ints. This will decay to a pointer to an array of 3 ints or int (*)[3]. This should explain the compile error that you are getting. You should declare put either as:

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

or as the completely equivalent:

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

Because you cannot pass arrays by value the compiler will automatically convert a function declaration which takes an array parameter to one which takes the equivalent pointer parameter.

I've changed the return type to void as you don't use or need the return value as you are passing the parameter by pointer. You should remove return b; from your definition of put.

Hint: Don't think of int[2][3] as a 2-d array but as an array of arrays.

情何以堪。 2024-11-12 00:17:07

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

也许你会发现这个有用:

Pass 2d array to function in C?

或这样:

C++ 从函数返回多维数组

You can not return a 2d-array from a function in C, you can only return a 1d-array of pointers to the first elements of a 2d-array.

Maybe you could find useful this:

Pass 2d array to function in C?

or this:

C++ Returning multidimension array from function

眼藏柔 2024-11-12 00:17:07

1.在使用函数之前必须先声明或定义该函数,这与其他流行语言不同。

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

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

1.you should declare or define the function before use it,it's different with other popular luanguage.

2.you do not need to return the pointer in the function put ,the data in array has be changed

3.you needed to notice the type ,the type of int array[][] is int **

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文