c++ copies parts of 2d array










-2















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();










share|improve this question



















  • 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 an array[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] are foo[0] and foo[1] but not foo[2].

    – Swordfish
    Nov 14 '18 at 13:18







  • 1





    @user463035818 it's make-believe anyway.

    – Swordfish
    Nov 14 '18 at 13:48















-2















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();










share|improve this question



















  • 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 an array[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] are foo[0] and foo[1] but not foo[2].

    – Swordfish
    Nov 14 '18 at 13:18







  • 1





    @user463035818 it's make-believe anyway.

    – Swordfish
    Nov 14 '18 at 13:48













-2












-2








-2


0






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();










share|improve this question
















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++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 an array[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] are foo[0] and foo[1] but not foo[2].

    – Swordfish
    Nov 14 '18 at 13:18







  • 1





    @user463035818 it's make-believe anyway.

    – Swordfish
    Nov 14 '18 at 13:48












  • 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 an array[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] are foo[0] and foo[1] but not foo[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












1 Answer
1






active

oldest

votes


















2














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





share|improve this answer

























  • 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 of array.

    – Swordfish
    Nov 14 '18 at 13:29











  • I have updated the image

    – sanchit verma
    Nov 14 '18 at 13:37










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
);



);













draft saved

draft discarded


















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









2














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





share|improve this answer

























  • 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 of array.

    – Swordfish
    Nov 14 '18 at 13:29











  • I have updated the image

    – sanchit verma
    Nov 14 '18 at 13:37















2














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





share|improve this answer

























  • 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 of array.

    – Swordfish
    Nov 14 '18 at 13:29











  • I have updated the image

    – sanchit verma
    Nov 14 '18 at 13:37













2












2








2







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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 of array.

    – 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











  • @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 of array.

    – 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党