LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++: playing with arrays+pointers -unexpected output (https://www.linuxquestions.org/questions/programming-9/c-playing-with-arrays-pointers-unexpected-output-579959/)

kpachopoulos 08-26-2007 12:38 PM

C++: playing with arrays+pointers -unexpected output
 
Hi,
here's the code:

Code:

#include <hash_map.h>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    //create an array of int pointers
    int** array=new int*[10];

    //make each one of the above pointers point to an int array
    //eventually create a 2D array of size 10x10
    for (int i=0; i<10; i++)
    {
        array[i]=new int[10];
    }
   
   
   
    //print the 2D array in 10-group values
    cout << "2D array BEFORE 0 INITIALIZATION" <<endl;
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {       
            printf("%i",*array[j]);   
        }
        cout<<endl;
        (*array)++;       
    }       
   
   
    //initialize the 2D array with 0 values
    for (int i=0; i<10; i++)
    {       
        for (int j=0; j<10; j++)
        {
            *array[j]=0;
        }       

        (*array)++;
    }
   
   
    //print the 2D array in 10-group values
    cout << "2D array BEFORE entering 1s and after initialization" <<endl;
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {       
            printf("%i",*array[j]);   
        }
        cout<<endl;
        (*array)++;       
    }   
   
   
    //enter the value 1 in every fifth slot
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {       
            if ((j+1)%5==0)
            {
                *array[j]=1;
            }
        }
       
        (*array)++;       
    }
   
   
    //print the 2D array in 10-group values
    cout << "2D array AFTER entering 1s" <<endl;
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {       
            printf("%i",*array[j]);   
        }
        cout<<endl;
        (*array)++;       
    }   
}

and here's the output:
Code:

2D array BEFORE 0 INITIALIZATION
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
2D array BEFORE entering 1s and after initialization
0000000000
0000000000
49000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
2D array AFTER entering 1s
0000100001
0000100001
0000100001
0000100001
0000100001
0000100001
49000100001
0000100001
1000100001
0000100001

Everything is OK, except this 49, which appears out of nowhere and in different place after enter the 1s. Can somebody please explain?

Thanks

JoeyAdams 08-26-2007 02:23 PM

First of all, get in the habit of freeing the arrays you make:

Code:

//free the array
        for (int i=0; i<10; i++)
        { delete[] array[i]; }
        delete[] array;

If you do this now, your program will crash because it isn't written correctly. When you say (*array)++; , that is the same as array[0]++, meaning increment the pointer at array[0]. You are trailing array[0] out way after its original location, so when you go to free it, the program will crash. Also, note the order of operations. [] comes before *, so when you say:

*array[j]=0;

That means *(array[j]), NOT (*array)[j] What you probably meant to write for each loop is this:

Code:

//print the 2D array in 10-group values
    cout << "2D array BEFORE 0 INITIALIZATION" <<endl;
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {       
            printf("%i",(*array)[j]);   
        }
                        cout<<endl;
        array++;
    }
    array-=10; /* important!  We iterated through the array by
adding one 10 times, so we need to subtract 10 so the pointer will
be back where it was when we do anything else with it. */



All times are GMT -5. The time now is 05:03 PM.