2

I've this code, where I want to create a dynamic 2-d array with n x 5 values -

int** bump = new int*[b_bump_num];
for(int i = 0; i < b_bump_num; i++) {
    bump[i] = new int[5];
}

for(int i = 0; i < b_bump_num; i++) {
    delete[] bump[i];
}
delete[] bump;

But I'm facing this error -

*error:Expressions must contain pointer types-The third last line-bump[i]

dou
  • 23
  • 4
  • which compiler are you using? Nothing wrong with your code. Please post the exact error message and a [mcve] – Alan Birtles Nov 20 '18 at 07:53
  • I am very sorry, I use this software for the first time, many things do not understand, thank you for explaining to me – dou Nov 20 '18 at 07:54
  • An error is reported on the The third last line line: the expression must contain pointer type – dou Nov 20 '18 at 07:56
  • 3
    @dou Unrelated: In C++ you have a better option. Its called [`std::vector<>`](https://en.cppreference.com/w/cpp/container/vector). And as usual, the question would be; is there any special reason that you are not using it? – JeJo Nov 20 '18 at 08:01
  • @JeJo:Can you give me an example of code usage?I do not have the programming foundation, a lot of basic knowledge do not understand, can only learn from the program example – dou Nov 20 '18 at 08:11
  • What is your filename and how are you compiling your program? – Rogus Nov 20 '18 at 08:12
  • @Rogus:My filename is cable.cpp,the tool is Micrisoft Visual Studio2012.I‘m not good at English. – dou Nov 20 '18 at 08:28
  • I know why I was wrong, because I had a problem with the call to bump.Wrong format :void cable(Mat I2,int *bump_num,int *bump).Right format :void cable(Mat I2,int *bump_num,int * *bump).Thank you very much for helping me. I'm sorry for not making it clear – dou Nov 20 '18 at 08:40
  • @dou I have added an example code for `std::vector`, read more about them in the provided link and additionally read more in a good book. Dynamic arrays are mainly handled by std::vectors in C++. The one you showed is a C coder option as C has no vector. – JeJo Nov 20 '18 at 08:43

1 Answers1

0

This may be all you need -

Approach 1: Using a simple 2-D array

#include <iostream>

using namespace std;

int main()
{
    int dimension1, dimension2 = 5;
    cout << "Enter first dimension : " << endl;
    cin >> dimension1;

    // dynamic allocation
    int** array2d = new int*[dimension1];
    for(int i = 0; i < dimension1; ++i)
        array2d[i] = new int[dimension2];

    /* 
    // you may fill it with your values
    for(int i = 0; i < dimension1; ++i)
        for(int j = 0; j < dimension2; ++j)
            array2d[i][j] = i;
    */

    // print
    for(int i = 0; i < dimension1; ++i) {
        for(int j = 0; j < dimension2; ++j) {
            cout << array2d[i][j] << " ";
        }
        cout << endl;
    }

    // free
    for(int i = 0; i < dimension1; ++i)
        delete [] array2d[i];

    delete [] array2d;

    return 0;
}

Sample Output :

Enter first dimension : 
4
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

Approach 2: Using Vectors -

#include <iostream>
#include <vector> 

using namespace std;

int main()
{
    // size of row 
    int rowSize;
    int colSize = 5;

    cout << "Enter row size : ";
    cin >> rowSize;

    // Create a vector of vector with size equal to rowSize; filled with 0.
    vector<vector<int>> vec(rowSize, vector<int>(colSize));

    for (int i = 0; i < rowSize; i++) { 
        for (int j = 0; j < vec[i].size(); j++) 
            cout << vec[i][j] << " "; 
        cout << endl; 
    }

    vec.clear();

    return 0;
}

Sample Output :

Enter row size : 3                  
0 0 0 0 0                           
0 0 0 0 0                           
0 0 0 0 0
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56