How to create an array of n rows and 5 columns in C++, but the n value is changing
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]
c++ arrays
|
show 4 more comments
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]
c++ arrays
which compiler are you using? Nothing wrong with your code. Please post the exact error message and a Minimal, Complete, and Verifiable example
– Alan Birtles
Nov 20 '18 at 7: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 7:54
An error is reported on the The third last line line: the expression must contain pointer type
– dou
Nov 20 '18 at 7:56
3
@dou Unrelated: In C++ you have a better option. Its calledstd::vector<>
. And as usual, the question would be; is there any special reason that you are not using it?
– JeJo
Nov 20 '18 at 8: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 8:11
|
show 4 more comments
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]
c++ arrays
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]
c++ arrays
c++ arrays
edited Nov 20 '18 at 8:00
dou
asked Nov 20 '18 at 6:49
doudou
134
134
which compiler are you using? Nothing wrong with your code. Please post the exact error message and a Minimal, Complete, and Verifiable example
– Alan Birtles
Nov 20 '18 at 7: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 7:54
An error is reported on the The third last line line: the expression must contain pointer type
– dou
Nov 20 '18 at 7:56
3
@dou Unrelated: In C++ you have a better option. Its calledstd::vector<>
. And as usual, the question would be; is there any special reason that you are not using it?
– JeJo
Nov 20 '18 at 8: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 8:11
|
show 4 more comments
which compiler are you using? Nothing wrong with your code. Please post the exact error message and a Minimal, Complete, and Verifiable example
– Alan Birtles
Nov 20 '18 at 7: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 7:54
An error is reported on the The third last line line: the expression must contain pointer type
– dou
Nov 20 '18 at 7:56
3
@dou Unrelated: In C++ you have a better option. Its calledstd::vector<>
. And as usual, the question would be; is there any special reason that you are not using it?
– JeJo
Nov 20 '18 at 8: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 8:11
which compiler are you using? Nothing wrong with your code. Please post the exact error message and a Minimal, Complete, and Verifiable example
– Alan Birtles
Nov 20 '18 at 7:53
which compiler are you using? Nothing wrong with your code. Please post the exact error message and a Minimal, Complete, and Verifiable example
– Alan Birtles
Nov 20 '18 at 7: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 7:54
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 7:54
An error is reported on the The third last line line: the expression must contain pointer type
– dou
Nov 20 '18 at 7:56
An error is reported on the The third last line line: the expression must contain pointer type
– dou
Nov 20 '18 at 7:56
3
3
@dou Unrelated: In C++ you have a better option. Its called
std::vector<>
. And as usual, the question would be; is there any special reason that you are not using it?– JeJo
Nov 20 '18 at 8:01
@dou Unrelated: In C++ you have a better option. Its called
std::vector<>
. And as usual, the question would be; is there any special reason that you are not using it?– JeJo
Nov 20 '18 at 8: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 8:11
@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 8:11
|
show 4 more comments
1 Answer
1
active
oldest
votes
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
2
Usestd::vector
.
– Fantastic Mr Fox
Nov 20 '18 at 7:36
if (b_bump_num == 0) { int bump[1]={0}; } else { int** bump=new int*[n]; 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;(An error is reported on the penultimate line: the expression must contain pointer type)
– dou
Nov 20 '18 at 7:45
@Tushar Walzade:Thank you very much.
– dou
Nov 20 '18 at 8:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387654%2fhow-to-create-an-array-of-n-rows-and-5-columns-in-c-but-the-n-value-is-changi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
2
Usestd::vector
.
– Fantastic Mr Fox
Nov 20 '18 at 7:36
if (b_bump_num == 0) { int bump[1]={0}; } else { int** bump=new int*[n]; 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;(An error is reported on the penultimate line: the expression must contain pointer type)
– dou
Nov 20 '18 at 7:45
@Tushar Walzade:Thank you very much.
– dou
Nov 20 '18 at 8:43
add a comment |
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
2
Usestd::vector
.
– Fantastic Mr Fox
Nov 20 '18 at 7:36
if (b_bump_num == 0) { int bump[1]={0}; } else { int** bump=new int*[n]; 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;(An error is reported on the penultimate line: the expression must contain pointer type)
– dou
Nov 20 '18 at 7:45
@Tushar Walzade:Thank you very much.
– dou
Nov 20 '18 at 8:43
add a comment |
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
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
edited Nov 20 '18 at 8:50
answered Nov 20 '18 at 7:21
Tushar WalzadeTushar Walzade
1,82331630
1,82331630
2
Usestd::vector
.
– Fantastic Mr Fox
Nov 20 '18 at 7:36
if (b_bump_num == 0) { int bump[1]={0}; } else { int** bump=new int*[n]; 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;(An error is reported on the penultimate line: the expression must contain pointer type)
– dou
Nov 20 '18 at 7:45
@Tushar Walzade:Thank you very much.
– dou
Nov 20 '18 at 8:43
add a comment |
2
Usestd::vector
.
– Fantastic Mr Fox
Nov 20 '18 at 7:36
if (b_bump_num == 0) { int bump[1]={0}; } else { int** bump=new int*[n]; 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;(An error is reported on the penultimate line: the expression must contain pointer type)
– dou
Nov 20 '18 at 7:45
@Tushar Walzade:Thank you very much.
– dou
Nov 20 '18 at 8:43
2
2
Use
std::vector
.– Fantastic Mr Fox
Nov 20 '18 at 7:36
Use
std::vector
.– Fantastic Mr Fox
Nov 20 '18 at 7:36
if (b_bump_num == 0) { int bump[1]={0}; } else { int** bump=new int*[n]; 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;(An error is reported on the penultimate line: the expression must contain pointer type)
– dou
Nov 20 '18 at 7:45
if (b_bump_num == 0) { int bump[1]={0}; } else { int** bump=new int*[n]; 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;(An error is reported on the penultimate line: the expression must contain pointer type)
– dou
Nov 20 '18 at 7:45
@Tushar Walzade:Thank you very much.
– dou
Nov 20 '18 at 8:43
@Tushar Walzade:Thank you very much.
– dou
Nov 20 '18 at 8:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387654%2fhow-to-create-an-array-of-n-rows-and-5-columns-in-c-but-the-n-value-is-changi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
which compiler are you using? Nothing wrong with your code. Please post the exact error message and a Minimal, Complete, and Verifiable example
– Alan Birtles
Nov 20 '18 at 7: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 7:54
An error is reported on the The third last line line: the expression must contain pointer type
– dou
Nov 20 '18 at 7:56
3
@dou Unrelated: In C++ you have a better option. Its called
std::vector<>
. And as usual, the question would be; is there any special reason that you are not using it?– JeJo
Nov 20 '18 at 8: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 8:11