C/C++ Pointers tutorial



Assume we have the following C/C++ statements

const int MAX_ROWS = 10;
const int MAX_COLS = 10;

int A[MAX_ROWS][MAX_COLS];
int actual_rows, actual_cols;

We have reserved 100 integer memory locations for our matrix, A. This is 100*2 bytes (for ANSI C integers).

Assume that this is reserved as contiguous memory starting at hex address fed0H. The data will be stored by rows.

When we access data in our matrix using the notation

A[i][j]

the location for this data is computed using

&A[0][0] + MAX_COLS * i + j

(note: only the second index size is used)

Some interesting information

&A[0][0] is the base address
A[0] " " " "
A " " " "
&A[0] " " " "

BUT these are not interchangeable, for instance:

&A[0][0] +1 points to A[0][1]
A[0] + 1 points to A[0][1]
A + 1 points to A[1][0]
(A + 1 is the same as A[1] and points to row 1)
&A[0] + 1 points to A[1][0]

Value equalities

A[i][j] = (*(A+i))[j] = *((*(A+i))+j) = *(A[i]+j)

SO

A[0][0] = (*(A))[0] = *((*A)+0) = *(A[0]+0)
A[0][2] = (*(A))[2] = *((*A)+2) = *(A[0]+2)
A[1][2] = (*(A+1))[2] = *((*(A+1))+2) = *(A[1]+2)

Address equalities

&A[i][j] = (A+i)[j] = *(A+i)+j = A[i]+j

SO

&A[0][0] = (A)[0] = *A+0 = A[0]+0
&A[0][2] = (A)[2] = *A+2 = A[0]+2
&A[1][2] = (A+1)[2] = (*A+1)+2 = A[1]+2

You can use this Turbo C++ program and prove to yourself that this is true.
Maybe you would like to play with some other ideas.
MATRICES & POINTERS

MATRICES & POINTERS

// A rose is a rose by any other name
// A value is a value by any other address style

#include 
#include 

main()
  {
//    int A[2][3];

    /* The storage map for this 2 dimensional array or matrix is
       A[0][0]
       A[0][1]
       A[0][2]
       A[1][0]
       A[1][1]
       A[1][2]
       That is C/C++ stores a matrix linearly in rows.
       The values for the matrix elements are referenced as
	  A[i][j] = (*(A+i))[j] = *((*(A+i))+j) = *(A[i]+j)
       The adresses for the matrix elements are
	  &A[i][j] = (A+i)[j] = (*(A+i))+j = A[i]+j             */

    // I want to initialize the matrix when I declare it
    int A[2][3] = { {1, 2, 3},
		     {4, 5, 6} };

// several ways to get the values
    //  C output
    printf("\nThe value of element A[0][0] is \n");
    printf("%d  %d  %d %d \n", A[0][0],(*(A+0))[0],*((*A)+0),*(A[0]+0) );

    // C++ output
    cout << "The value of element A[0][0] is \n"
	 << A[0][0] <<  "  " << (*(A))[0]  << "  " <<  *((*A)+0)
	 << "  " << *(A[0]+0) << "\n";
    cout << "The value of element A[0][2] is \n"
	 << A[0][2] <<  "  " << (*(A))[2]  << "  " <<  *((*A)+2)
	 << "  " << *(A[0]+2) << "\n";
    cout << "The value of element A[1][2] is \n"
	 << A[1][2] <<  "  " << (*(A+1))[2]  << "  " <<  *((*(A+1))+2)
	 << "  " << *(A[1]+2) << "\n";

// several ways to address the elements
    //  C output
    printf("\nThe address of element A[0][0] is \n");
    printf("%0x  %0x  %0x  %0x\n",&A[0][0],(A)[0],(*A+0),(A[0]+0));

    // C++ output
    cout << "The address of element A[0][0] is \n"
	 << &A[0][0] <<  "  " << &(*(A))[0]  << "  " << *(A)+0
	 << "  " << A[0]+0 << "\n";
    cout << "The address of element A[0][2] is \n"
	 << &A[0][2] <<  "  " << &(*(A))[2]  << "  " << *(A)+2
	 << "  " << A[0]+2 << "\n";
    cout << "The address of element A[1][2] is \n"
	 << &A[1][2] <<  "  " << &(*(A+1))[2]  << "  " << *(A+1)+2
	 << "  " << A[1]+2 << "\n";

// Notice how these addresses work
cout << "\nNotice what points to where \n";
cout << &A[0][0]+1 << " " << A[0]+1 << " " << A+1 << " " << &A[0]+1;
cout << "\n interesting? \n";
    return(0);
  }