c++ copies parts of 2d array
I have written a big program and it has some unexpected behavior so I've made another smaller program to test the issue.
My problem is that for some reason in a 2x2 array whenever I cin
element [0][2]
it also copies to the element [1][0]
For example if my array is
000
000
000
and I input the top right element to 'A' (say) then the middle left element also changes to 'A' and vice versa.
The same issue exists for the elements at [1][2]
and [2][0]
Here is my code:
#include<iostream>
using namespace std ;
char array[2][2];
void display()
cout<<endl;
for (int i=0;i<3;i++ )
for (int j=0;j<3;j++ )
cout<<array[i][j];
cout<<endl;
int main ()
for (int i=0;i<3;i++ )
for (int j=0;j<3;j++ )
array[i][j]='0';
display();
cin>>array[0][2];
display();
c++
|
show 9 more comments
I have written a big program and it has some unexpected behavior so I've made another smaller program to test the issue.
My problem is that for some reason in a 2x2 array whenever I cin
element [0][2]
it also copies to the element [1][0]
For example if my array is
000
000
000
and I input the top right element to 'A' (say) then the middle left element also changes to 'A' and vice versa.
The same issue exists for the elements at [1][2]
and [2][0]
Here is my code:
#include<iostream>
using namespace std ;
char array[2][2];
void display()
cout<<endl;
for (int i=0;i<3;i++ )
for (int j=0;j<3;j++ )
cout<<array[i][j];
cout<<endl;
int main ()
for (int i=0;i<3;i++ )
for (int j=0;j<3;j++ )
array[i][j]='0';
display();
cin>>array[0][2];
display();
c++
5
"in a [3][3] array" - Your array is not 3x3, it's 2x2.
– Yksisarvinen
Nov 14 '18 at 13:10
1
You have undefined behavior as you go out of bounds of the arrays you have. The number when you declare an array is not the top index, it's the number of elements.
– Some programmer dude
Nov 14 '18 at 13:12
2
Your confusion probably originates from:array[2]
calls the third element but creating anarray[2]
will give you 2 Elements of size.
– Stack Danny
Nov 14 '18 at 13:16
1
@sanchitverma yes, valid indexes for anint foo[2]
arefoo[0]
andfoo[1]
but notfoo[2]
.
– Swordfish
Nov 14 '18 at 13:18
1
@user463035818 it's make-believe anyway.
– Swordfish
Nov 14 '18 at 13:48
|
show 9 more comments
I have written a big program and it has some unexpected behavior so I've made another smaller program to test the issue.
My problem is that for some reason in a 2x2 array whenever I cin
element [0][2]
it also copies to the element [1][0]
For example if my array is
000
000
000
and I input the top right element to 'A' (say) then the middle left element also changes to 'A' and vice versa.
The same issue exists for the elements at [1][2]
and [2][0]
Here is my code:
#include<iostream>
using namespace std ;
char array[2][2];
void display()
cout<<endl;
for (int i=0;i<3;i++ )
for (int j=0;j<3;j++ )
cout<<array[i][j];
cout<<endl;
int main ()
for (int i=0;i<3;i++ )
for (int j=0;j<3;j++ )
array[i][j]='0';
display();
cin>>array[0][2];
display();
c++
I have written a big program and it has some unexpected behavior so I've made another smaller program to test the issue.
My problem is that for some reason in a 2x2 array whenever I cin
element [0][2]
it also copies to the element [1][0]
For example if my array is
000
000
000
and I input the top right element to 'A' (say) then the middle left element also changes to 'A' and vice versa.
The same issue exists for the elements at [1][2]
and [2][0]
Here is my code:
#include<iostream>
using namespace std ;
char array[2][2];
void display()
cout<<endl;
for (int i=0;i<3;i++ )
for (int j=0;j<3;j++ )
cout<<array[i][j];
cout<<endl;
int main ()
for (int i=0;i<3;i++ )
for (int j=0;j<3;j++ )
array[i][j]='0';
display();
cin>>array[0][2];
display();
c++
c++
edited Nov 14 '18 at 13:46
user463035818
17.2k42766
17.2k42766
asked Nov 14 '18 at 13:08
sanchit vermasanchit verma
12
12
5
"in a [3][3] array" - Your array is not 3x3, it's 2x2.
– Yksisarvinen
Nov 14 '18 at 13:10
1
You have undefined behavior as you go out of bounds of the arrays you have. The number when you declare an array is not the top index, it's the number of elements.
– Some programmer dude
Nov 14 '18 at 13:12
2
Your confusion probably originates from:array[2]
calls the third element but creating anarray[2]
will give you 2 Elements of size.
– Stack Danny
Nov 14 '18 at 13:16
1
@sanchitverma yes, valid indexes for anint foo[2]
arefoo[0]
andfoo[1]
but notfoo[2]
.
– Swordfish
Nov 14 '18 at 13:18
1
@user463035818 it's make-believe anyway.
– Swordfish
Nov 14 '18 at 13:48
|
show 9 more comments
5
"in a [3][3] array" - Your array is not 3x3, it's 2x2.
– Yksisarvinen
Nov 14 '18 at 13:10
1
You have undefined behavior as you go out of bounds of the arrays you have. The number when you declare an array is not the top index, it's the number of elements.
– Some programmer dude
Nov 14 '18 at 13:12
2
Your confusion probably originates from:array[2]
calls the third element but creating anarray[2]
will give you 2 Elements of size.
– Stack Danny
Nov 14 '18 at 13:16
1
@sanchitverma yes, valid indexes for anint foo[2]
arefoo[0]
andfoo[1]
but notfoo[2]
.
– Swordfish
Nov 14 '18 at 13:18
1
@user463035818 it's make-believe anyway.
– Swordfish
Nov 14 '18 at 13:48
5
5
"in a [3][3] array" - Your array is not 3x3, it's 2x2.
– Yksisarvinen
Nov 14 '18 at 13:10
"in a [3][3] array" - Your array is not 3x3, it's 2x2.
– Yksisarvinen
Nov 14 '18 at 13:10
1
1
You have undefined behavior as you go out of bounds of the arrays you have. The number when you declare an array is not the top index, it's the number of elements.
– Some programmer dude
Nov 14 '18 at 13:12
You have undefined behavior as you go out of bounds of the arrays you have. The number when you declare an array is not the top index, it's the number of elements.
– Some programmer dude
Nov 14 '18 at 13:12
2
2
Your confusion probably originates from:
array[2]
calls the third element but creating an array[2]
will give you 2 Elements of size.– Stack Danny
Nov 14 '18 at 13:16
Your confusion probably originates from:
array[2]
calls the third element but creating an array[2]
will give you 2 Elements of size.– Stack Danny
Nov 14 '18 at 13:16
1
1
@sanchitverma yes, valid indexes for an
int foo[2]
are foo[0]
and foo[1]
but not foo[2]
.– Swordfish
Nov 14 '18 at 13:18
@sanchitverma yes, valid indexes for an
int foo[2]
are foo[0]
and foo[1]
but not foo[2]
.– Swordfish
Nov 14 '18 at 13:18
1
1
@user463035818 it's make-believe anyway.
– Swordfish
Nov 14 '18 at 13:48
@user463035818 it's make-believe anyway.
– Swordfish
Nov 14 '18 at 13:48
|
show 9 more comments
1 Answer
1
active
oldest
votes
If you want to access up to array[2][2]
your array has to be defined as char array[3][3];
.
Valid indexes for an array T foo[SIZE]
are 0 ... SIZE - 1
.
Please note that array
is a very bad name since there is also std::array<>
and you are using using namespace std;
which will spill out all symbols from the namespace std into the global namespace.
With
#include <iostream>
using namespace std;
char array[3][3];
void display()
cout << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cout << array[i][j];
cout << endl;
int main()
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
array[i][j] = '0';
display();
cin >> array[0][2];
display();
it works as expected:
Output:
000
000
000
A
00A
000
000
I just tried changing the array to [3][3] ...still no progress :(
– sanchit verma
Nov 14 '18 at 13:19
@sanchitverma I don't believe you. See my edit.
– Swordfish
Nov 14 '18 at 13:21
please see my edit on the question
– sanchit verma
Nov 14 '18 at 13:27
@sanchitverma Your screenshot doesn't show the definition ofarray
.
– Swordfish
Nov 14 '18 at 13:29
I have updated the image
– sanchit verma
Nov 14 '18 at 13:37
|
show 7 more comments
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%2f53301000%2fc-copies-parts-of-2d-array%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
If you want to access up to array[2][2]
your array has to be defined as char array[3][3];
.
Valid indexes for an array T foo[SIZE]
are 0 ... SIZE - 1
.
Please note that array
is a very bad name since there is also std::array<>
and you are using using namespace std;
which will spill out all symbols from the namespace std into the global namespace.
With
#include <iostream>
using namespace std;
char array[3][3];
void display()
cout << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cout << array[i][j];
cout << endl;
int main()
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
array[i][j] = '0';
display();
cin >> array[0][2];
display();
it works as expected:
Output:
000
000
000
A
00A
000
000
I just tried changing the array to [3][3] ...still no progress :(
– sanchit verma
Nov 14 '18 at 13:19
@sanchitverma I don't believe you. See my edit.
– Swordfish
Nov 14 '18 at 13:21
please see my edit on the question
– sanchit verma
Nov 14 '18 at 13:27
@sanchitverma Your screenshot doesn't show the definition ofarray
.
– Swordfish
Nov 14 '18 at 13:29
I have updated the image
– sanchit verma
Nov 14 '18 at 13:37
|
show 7 more comments
If you want to access up to array[2][2]
your array has to be defined as char array[3][3];
.
Valid indexes for an array T foo[SIZE]
are 0 ... SIZE - 1
.
Please note that array
is a very bad name since there is also std::array<>
and you are using using namespace std;
which will spill out all symbols from the namespace std into the global namespace.
With
#include <iostream>
using namespace std;
char array[3][3];
void display()
cout << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cout << array[i][j];
cout << endl;
int main()
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
array[i][j] = '0';
display();
cin >> array[0][2];
display();
it works as expected:
Output:
000
000
000
A
00A
000
000
I just tried changing the array to [3][3] ...still no progress :(
– sanchit verma
Nov 14 '18 at 13:19
@sanchitverma I don't believe you. See my edit.
– Swordfish
Nov 14 '18 at 13:21
please see my edit on the question
– sanchit verma
Nov 14 '18 at 13:27
@sanchitverma Your screenshot doesn't show the definition ofarray
.
– Swordfish
Nov 14 '18 at 13:29
I have updated the image
– sanchit verma
Nov 14 '18 at 13:37
|
show 7 more comments
If you want to access up to array[2][2]
your array has to be defined as char array[3][3];
.
Valid indexes for an array T foo[SIZE]
are 0 ... SIZE - 1
.
Please note that array
is a very bad name since there is also std::array<>
and you are using using namespace std;
which will spill out all symbols from the namespace std into the global namespace.
With
#include <iostream>
using namespace std;
char array[3][3];
void display()
cout << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cout << array[i][j];
cout << endl;
int main()
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
array[i][j] = '0';
display();
cin >> array[0][2];
display();
it works as expected:
Output:
000
000
000
A
00A
000
000
If you want to access up to array[2][2]
your array has to be defined as char array[3][3];
.
Valid indexes for an array T foo[SIZE]
are 0 ... SIZE - 1
.
Please note that array
is a very bad name since there is also std::array<>
and you are using using namespace std;
which will spill out all symbols from the namespace std into the global namespace.
With
#include <iostream>
using namespace std;
char array[3][3];
void display()
cout << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cout << array[i][j];
cout << endl;
int main()
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
array[i][j] = '0';
display();
cin >> array[0][2];
display();
it works as expected:
Output:
000
000
000
A
00A
000
000
edited Nov 14 '18 at 13:30
answered Nov 14 '18 at 13:16
SwordfishSwordfish
9,38811436
9,38811436
I just tried changing the array to [3][3] ...still no progress :(
– sanchit verma
Nov 14 '18 at 13:19
@sanchitverma I don't believe you. See my edit.
– Swordfish
Nov 14 '18 at 13:21
please see my edit on the question
– sanchit verma
Nov 14 '18 at 13:27
@sanchitverma Your screenshot doesn't show the definition ofarray
.
– Swordfish
Nov 14 '18 at 13:29
I have updated the image
– sanchit verma
Nov 14 '18 at 13:37
|
show 7 more comments
I just tried changing the array to [3][3] ...still no progress :(
– sanchit verma
Nov 14 '18 at 13:19
@sanchitverma I don't believe you. See my edit.
– Swordfish
Nov 14 '18 at 13:21
please see my edit on the question
– sanchit verma
Nov 14 '18 at 13:27
@sanchitverma Your screenshot doesn't show the definition ofarray
.
– Swordfish
Nov 14 '18 at 13:29
I have updated the image
– sanchit verma
Nov 14 '18 at 13:37
I just tried changing the array to [3][3] ...still no progress :(
– sanchit verma
Nov 14 '18 at 13:19
I just tried changing the array to [3][3] ...still no progress :(
– sanchit verma
Nov 14 '18 at 13:19
@sanchitverma I don't believe you. See my edit.
– Swordfish
Nov 14 '18 at 13:21
@sanchitverma I don't believe you. See my edit.
– Swordfish
Nov 14 '18 at 13:21
please see my edit on the question
– sanchit verma
Nov 14 '18 at 13:27
please see my edit on the question
– sanchit verma
Nov 14 '18 at 13:27
@sanchitverma Your screenshot doesn't show the definition of
array
.– Swordfish
Nov 14 '18 at 13:29
@sanchitverma Your screenshot doesn't show the definition of
array
.– Swordfish
Nov 14 '18 at 13:29
I have updated the image
– sanchit verma
Nov 14 '18 at 13:37
I have updated the image
– sanchit verma
Nov 14 '18 at 13:37
|
show 7 more comments
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%2f53301000%2fc-copies-parts-of-2d-array%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
5
"in a [3][3] array" - Your array is not 3x3, it's 2x2.
– Yksisarvinen
Nov 14 '18 at 13:10
1
You have undefined behavior as you go out of bounds of the arrays you have. The number when you declare an array is not the top index, it's the number of elements.
– Some programmer dude
Nov 14 '18 at 13:12
2
Your confusion probably originates from:
array[2]
calls the third element but creating anarray[2]
will give you 2 Elements of size.– Stack Danny
Nov 14 '18 at 13:16
1
@sanchitverma yes, valid indexes for an
int foo[2]
arefoo[0]
andfoo[1]
but notfoo[2]
.– Swordfish
Nov 14 '18 at 13:18
1
@user463035818 it's make-believe anyway.
– Swordfish
Nov 14 '18 at 13:48