Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to modify 2d array in function c++

void Insert_into_2D_Array(int** foo, int x_pos, int y_pos, int x_size, int y_size)
{
  int insert_value = 10; 

  if (x_pos < x_size && y_pos < y_size) {
    foo[x_pos][y_pos] = insert_value;    // insert_value lost post func exit?
  }
}

void Init_2D_Array(int** foo, int x_size, int y_size)
{

  foo = new int*[x_size];    // new alloc mem lost post func exit ?
  for (int i=0;i<x_size;i++)
  {
      foo[i] = new int[y_size];     // new alloc mem lost post func exit
  }
}

int main(int agc, char** argv)
{

  int** foo; 
  int x_size=10, y_size=10;   
  Init_2D_Array(foo, x_size, y_size); 
  Insert_into_2D_Array(foo, 3,3, x_size, y_size); 

}
Comment

how to modify 2d array in function c++

void Insert_into_2D_Array(int** foo, int x_pos, int y_pos, int x_size, int y_size)
{
  int insert_value = 10000;

  if (x_pos < x_size && y_pos < y_size) {
    (foo)[x_pos][y_pos] = insert_value;    // insert_value lost post func exit
  }
}

void Init_2D_Array(int*** foo, int x_size, int y_size)
{

  *foo = new int*[x_size];    // new alloc mem lost post func exit
  for (int i=0;i<x_size;i++)
  {
      (*foo)[i] = new int[y_size];     // new alloc mem lost post func exit
  }
}

void main(){

      int** foo = NULL;
      int x_size=10, y_size=10;
      Init_2D_Array(&foo, x_size, y_size);
      Insert_into_2D_Array(foo, 3,3, x_size, y_size);

      cout<<"#############  "<<foo[3][3]<<endl;
}
Comment

how to modify 2d array in function c++

class Array2D
{
private:
    int* m_array;
    int m_sizeX;
    int m_sizeY;

public:
    Array2D(int sizeX, int sizeY) : m_sizeX(sizeX), m_sizeY(sizeY)
    {
        m_array = new int[sizeX*sizeY];
    }

    ~Array2D()
    {
        delete[] m_array;
    }

    int & at(int x, int y)
    {
        return m_array[y*sizeX + x];
    }
};
Comment

how to modify 2d array in function c++

void Insert_into_2D_Array(int** foo, int x_pos, int y_pos, int x_size, int y_size)
{
    int insert_value = 10;

    if (x_pos < x_size && y_pos < y_size) {
        foo[x_pos][y_pos] = insert_value;    // insert_value lost post func exit
    }
}

int** Init_2D_Array(int x_size, int y_size)
{

    int** foo = new int*[x_size];    // new alloc mem lost post func exit  
    for (int i = 0; i<x_size; i++)
    {
        foo[i] = new int[y_size];     // new alloc mem lost post func exit
    }

    return foo;
}

int main()
{

    int** foo;
    int x_size = 10, y_size = 10;
    foo = Init_2D_Array(x_size, y_size);
    Insert_into_2D_Array(foo, 3, 3, x_size, y_size);

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: if else c++ 
Cpp :: how to set arrays as function parameters in c++ 
Cpp :: c++ comment 
Cpp :: compile and run cpp file on mac c++ 
Cpp :: queue in cpp 
Cpp :: deque 
Cpp :: An Array declaration by initializing elements in C++ 
Cpp :: map of maps c++ 
Cpp :: std::enable_shared_from_this include 
Cpp :: vsearch c program stdlib 
Cpp :: Consell de forces polítiques de Catalunya 
Cpp :: recherche recursive le max dans une liste 
Cpp :: Maximum Weight Difference codechef solution c++ 
Cpp :: how to scan vector in c++ 
Cpp :: c++ solver online free 
Cpp :: c++ break statement 
Cpp :: copy file to vector c++ 
Cpp :: what is xor_eq c++ 
Cpp :: error when using base class members 
Cpp :: C++ Display a text 5 times 
Cpp :: default parameter c++ a field 
Cpp :: Missing GL version 
Cpp :: PCL normal specific point 
Cpp :: facade pattern C++ code 
Cpp :: c++ correct upto 3 decimal places 
Cpp :: default order in set in c++ 
Cpp :: the statement vector vector int matrix(100 vector int (50 100) ) declares 
Cpp :: what is require to run min max function on linux in cpp 
Cpp :: c++ string to vector using delimiter 
Cpp :: c++ caps lock key 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =