C++ gurus and learners, we program ourselves :P all work is CC licensed

Wednesday, November 13

Two Dimension Pointers C++

Pointers are very useful construct of C++ programming language and what is more useful is 2d pointers.

The pointers are special variables pointing to a memory location or a variable where as an array is a collection of those variables. A 2d pointer is a pointer which is pointing to array of pointers.

e.g.

//Assign an array of 5 nameless pointers to a 2d pointer
int **pointer = new int*[5];

//then assign values to each array
for(int i=0;i<5; i++)
{
    pointer[] = new int[10];
}

//now we have a 2d pointer which can be also accessed as array
pointer[2][9] ; //this is third row and 10th element

7 comments :

  1. a correct io in line13 here pointer[i] has to be written

    ReplyDelete
  2. dun knw who shared it but worth reading post.

    ReplyDelete
  3. A post for starters of arrays and pointers :P

    ReplyDelete
  4. Nice information and thanks for posting this here! Please keep sharing more
    information like this further in future. You can find more information on C++
    Tutorials in the following link..

    CPP Tutorials

    ReplyDelete