Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4576310
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:11:15+00:00 2026-05-21T20:11:15+00:00

Hi I’m new to c language i hava a problem : i want to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T20:11:15+00:00Added an answer on May 21, 2026 at 8:11 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I want to construct a data frame in an Rcpp function, but when I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.