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 6825761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:04:11+00:00 2026-05-26T22:04:11+00:00

I messed around with this enough but I really don’t get it. Here is

  • 0

I messed around with this enough but I really don’t get it.

Here is what I want to do: Take a 2D char array as an input in a function, change the values in it and then return another 2D char array.

That’s it. Quite simple idea, but ideas do not get to work easily in C.

Any idea to get me started in its simplest form is appreciated. Thanks.

  • 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-26T22:04:12+00:00Added an answer on May 26, 2026 at 10:04 pm

    C will not return an array from a function.

    You can do several things that might be close enough:

    • You can package your array in struct and return that. C will return structs from functions just fine. The downside is this can be a lot of memory copying back and forth:

      struct arr {
          int arr[50][50];
      }
      
      struct arr function(struct arr a) {
          struct arr result;
          /* operate on a.arr[i][j]
             storing into result.arr[i][j] */
          return result;
      }
      
    • You can return a pointer to your array. This pointer must point to memory you allocate with malloc(3) for the array. (Or another memory allocation primitive that doesn’t allocate memory from the stack.)

      int **function(int param[][50]) {
          int arr[][50] = malloc(50 * 50 * sizeof int);
          /* store into arr[i][j] */
          return arr;
      }
      
    • You can operate on the array pointer passed into your function and modify the input array in place.

      void function(int param[][50]) {
          /* operate on param[i][j] directly -- destroys input */
      }
      
    • You can use a parameter as an “output variable” and use that to “return” the new array. This is best if you want the caller to allocate memory or if you want to indicate success or failure:

      int output[][50];
      
      int function(int param[][50], int &output[][50]) {
          output = malloc(50 * 50 * sizeof int);
          /* write into output[i][j] */
          return success_or_failure;
      }
      

      Or, for the caller to allocate:

      int output[50][50];
      
      void function(int param[][50], int output[][50]) {
          /* write into output[i][j] */
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK I'm redoing this since I messed it up. :) here's the xaml. <Window
I've messed around with this teeny little detail for hours now and I've finally
I'm new to Linux and I've only ever messed around with it but now
I have not done any large game development projects, only messed around with little
After I messed up the description of my previous post on this I have
I have been trying to work out the bug on this but can't seem
I'm having some fun trying to get my head around some MVP stuf, as
I know, it's pathetic, but IT just got around to installing IE8 on my
I haven't been able to figure this out despite hours of messing around, hopefully
I've messed around with PreviewLostKeyboardFocus which almost gets you there. I've seen a couple

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.