开发者

c language array problems

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 de开发者_如何学运维claration 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


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!


where are you storing the return value from put ?

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


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.


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


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 **

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜