LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-26-2007, 12:38 PM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Rep: Reputation: 30
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

Last edited by kpachopoulos; 08-26-2007 at 12:38 PM. Reason: wrong closing tag
 
Old 08-26-2007, 02:23 PM   #2
JoeyAdams
Member
 
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
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. */

Last edited by JoeyAdams; 08-26-2007 at 02:24 PM. Reason: Broke comment into lines so forum window wouldn't be wide
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
C: malloc arrays- unexpected output kpachopoulos Programming 3 03-24-2006 09:30 PM
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 08:24 AM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 05:13 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM


All times are GMT -5. The time now is 04:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration