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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:14:00+00:00 2026-05-31T06:14:00+00:00

I am trying to modify the contents of a 2D array in C++ using

  • 0

I am trying to modify the contents of a 2D array in C++ using a function. I haven’t been able to find information on how to pass a 2D array to a function by reference and then manipulate individual cells.

The problem I am trying to solve has the following format. I have made a simple program for brevity.

#include<cstdlib>
#include<iostream>
using namespace std;

void func(int& mat) {
    int k,l;
    for(k=0;k<=2;k++) {
    for(l=0;l<=2;l++) {
    mat[k][l]=1;  //This is incorrect because mat is just a reference, but
                  // this is the kind of operation I want. 
    }
}

return; 
}

int main() {
int A[3][3];
int i, j;
char jnk;

for(i=0;i<=2;i++) {
    for(j=0;j<=2;j++) {
        A[i][j]=0;
    }
}

    func(A);

cout << A[0][0];
    return 0;
}

So the value of A[0][0] should change from 0 to 1. What is the correct way to do this? Many thanks in advance…

  • 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-31T06:14:01+00:00Added an answer on May 31, 2026 at 6:14 am

    Arrays are not passed by value, so you can simply use

    void func(int mat[][3])
    

    and, if you modify the values of mat inside func you are actually modifying it in main.

    You can use that approach if you know a priori the size of your matrix, otherwise consider working with pointers:

    #include <iostream>
    
    void f(int **m, int r, int c) {
        m[0][0]=1;
    }
    
    int main () {
    
        int **m;
        int r=10,c=10;
        int i;
    
        m = (int**)malloc(r*sizeof(int*));
    
        for (i=0; i<r;i++)
            m[i] = (int*)malloc(c*sizeof(int));
    
        f(m,r,c);
    
        printf("%d\n",m[0][0]);
    
        for(i=0;i<r;i++)
            free(m[i]);
    
        free(m);
    
        return 0;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to modify the contents of an MSI file using Powershell. To do
I've been trying to modify this method from redirecting and returning the contents of
I'm trying to modify the contents of one pointer using another. I want string2
I am trying to modify a value in an array using the C programming
I am trying to modify the controls of a Panel, have it update, then
I've been trying to modify the AuditTrail code so that it does not copy
I've been attempting to modify the contents of a custom tree view that inherits
I'm trying to find a way to modify marginTop for an EditText by code...I
I'm trying to modify my GreaseMonkey script from firing on window.onload to window.DOMContentLoaded, but
I'm trying to modify the class of an element if an ajax call based

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.