INCREMENTING POINTERS TO POINTERS (**p) AND POINTERS TO POINTERS TO POINTERS (***p) IN C PROGRAMMING IN ACCESSING MULTI-DIMENTIONAL ARRAY

WHAT IS A POINTER?: A pointer is a variable pointed to the address in memory of another variable.

POINTER DECLARATION OR CREATING A POINTER.

typename *pointername;

char *ip;
int *ip;
short *ip;
long *ip;
float *ip;
double *ip;

*ip; is just a sample you can use any letter combination like. char *message; int *rate; etc..etc..

HOW TO INITIALIZE POINTER?

You have to use & the (ampersand) or ADDRESS-OF OPERATOR(&)

ip = &rate;

Example:

int *ip;
int rate = 10;

*ip = rate;

You initialize using ip = &rate.

Now the value of *ip and rate = 10; and the ip and &rate is the value of ADDRESS IN MEMORY.

REMEMBER:AN ARRAY NAME IS THE POINTER IN FIRST ELEMENT OF AN ARRAY.

An array name without brackets is a pointer to the array’s first element. Meaning the name of your array is the pointer to the first element of an array.

example: arr[1] = {1,2};

The value of arr = 1;

NOW I AM GOING TO DEMONSTRATE TO YOU THE POINTERS MANIPULATION, DETERMINING THE SIZE OF ARRAY, THE ADDRESS LOCATION IN MEMORY OF THAT ARRAY.

/*************

HERE’S MY CODE.

**************/

#include <stdio.h>

main()
{

int multi[2][3]= {{10,20,30},
{40,50,60}
};

printf("Given an array int multi[2][3]= {{10,20,30},{40,50,60}}\n");

/* Determining the sizes of Elements of multi-dimensional array */

printf("\nDetermining the sizes of Elements\n");

printf("The size of multi = %u\n", sizeof(multi));
printf("The size of multi[0] = %u\n", sizeof(multi[0]));
printf("The size of multi[0][0] = %u\n", sizeof(multi[0][0]));

printf("\nPointer Location in Memory of Multi-Dimentional Array in Hex(%x) use (%d) if you want in decimal value \n");

/* Pointer Value in Memory */
printf("The value of (multi) = %x\n", multi);
printf("The value of (multi + 1) = %x\n", (multi+1));
printf("The value of &amp;multi[1] = %x\n", &amp;multi[1]);

//change %x above to %d to see the decimal value

printf("\nLet us get the 1st element of Multi-Dimentional Array\n");
printf("%d\n", multi[0][0]);
printf("%d\n", *multi[0]);
<strong>printf("%d", **multi); //------&gt;POINTERS TO POINTERS.</strong>

printf("\n Let us get the 2nd element of Multi-Dimentional Array\n");

printf("%d\n", multi[0][1]);
printf("%d\n", *(multi[0]+1));
printf("%d", *(*multi + 1));

printf("\n Let us get the 3rd element of Multi-Dimentional Array\n");

printf("%d\n", multi[0][2]);
printf("%d\n", *(multi[0]+2));
printf("%d", *(*multi + 2));

printf("\n Let us get the 4th element of Multi-Dimentional Array\n");

printf("%d\n", multi[0][3]);
printf("%d\n", *(multi[0]+3));
printf("%d", *(*multi + 3));

printf("\n Let us get the 5th element of Multi-Dimentional Array\n");

printf("%d\n", multi[0][4]);
printf("%d\n", *(multi[0]+4));
printf("%d", *(*multi + 4));

printf("\n Let us get the 6th element of Multi-Dimentional Array\n");

printf("%d\n", multi[0][5]);
printf("%d\n", *(multi[0]+5));
printf("%d", *(*multi + 5));

return 0;
}

/***********

THE OUTPUT:

************/Given an array int multi[2][3]= {{10,20,30},{40,50,60}}

Determining the sizes of Elements
The size of multi = 24
The size of multi[0] = 12
The size of multi[0][0] = 4

Pointer Location in Memory of Multi-Dimentional Array in Hex(4) use (0) if you want in decimal value
The value of (multi) = 11fffa8
The value of (multi + 1) = 11fffb4
The value of &multi[1] = 11fffb4

Let us get the 1st element of Multi-Dimentional Array
10
10
10
Let us get the 2nd element of Multi-Dimentional Array
20
20
20
Let us get the 3rd element of Multi-Dimentional Array
30
30
30
Let us get the 4th element of Multi-Dimentional Array
40
40
40
Let us get the 5th element of Multi-Dimentional Array
50
50
50
Let us get the 6th element of Multi-Dimentional Array
60
60
60

/**************

NOW LET US TRY POINTERS TO POINTERS TO POINTER (***P) IN THREE DIMENTIONAL ARRAY.

***************/

#include <stdio.h>

main()
{

int multi[2][3][2] = {
{ {155, 255}, {30, 40}, {50, 60} },
{ {70, 80}, {90, 100}, {110, 120} }
};

printf("Given 3Dimentional Array = int multi[2][3][2] = { { {155, 255}, {30, 40}, {50, 60} }, { {70, 80}, {90, 100}, {110, 120} } }\n");

/* Determining the sizes of Elements of multi-dimensional array */

printf("\nDetermining the sizes of Elements\n");

printf("The size of multi = %u\n", sizeof(multi));
printf("The size of multi[0] = %u\n", sizeof(multi[0]));
printf("The size of multi[0][0] = %u\n", sizeof(multi[0][0]));
printf("The size of multi[0][0][0] = %u\n", sizeof(multi[0][0][0]));

printf("\nPointer Location in Memory of Three-Dimentional Array in Hex(%x) use (%d) if you want in decimal value \n");

/* Pointer Value in Memory */
printf("The value of (multi) = %x\n", multi);
printf("The value of (multi + 1) = %x\n", (multi+1));
printf("The value of &amp;multi[1][1] = %x\n", &amp;multi[1][1]);
printf("The value of &amp;multi[1][1][1] = %x\n", &amp;multi[1][1][1]);

//change %x above to %d to see the decimal value

printf("The 1st value of THREE-dimentional array\n");

printf("\nLet us get the 1st element of Multi-Dimentional Array\n");
printf("%d\n", multi[0][0][0]);
printf("%d\n", *multi[0][0]);
printf("%d\n", **multi[0]);
<strong>printf("%d\n", ***multi); //---&gt; POINTERS TO POINTERS TO POINTERS</strong>

printf("\nThe 2nd value of THREE-Dimentional array\n");

printf("%d\n", multi[0][0][1]);
printf("%d\n", *(multi[0][0]+1));
printf("%d\n", *(*multi[0]+1));
printf("%d\n", *(**multi + 1));

return 0;
}

/***********
OUTPUT:
***********/

Given 3Dimentional Array = int multi[2][3][2] = { { {155, 255}, {30, 40}, {50, 60} }, { {70, 80}, {90, 100}, {110, 120} } }

Determining the sizes of Elements
The size of multi = 48
The size of multi[0] = 24
The size of multi[0][0] = 8
The size of multi[0][0][0] = 4

Pointer Location in Memory of Three-Dimentional Array in Hex(4) use (0) if you want in decimal value
The value of (multi) = 11fff90
The value of (multi + 1) = 11fffa8
The value of &multi[1][1] = 11fffb0
The value of &multi[1][1][1] = 11fffb4
The 1st value of THREE-dimentional array

Let us get the 1st element of Multi-Dimentional Array
155
155
155
155

The 2nd value of THREE-Dimentional array
255
255
255
255

 

 

NOW LET US TRY TO CREATE FUNCTION WORKING WITH POINTERS.

#include <stdio.h>

int sum( int *p1, int *p2); //FUNCTION PROTOTYPE see below function.

main()
{

int multi[2][3][2] = {
{ {500, 255}, {30, 40}, {50, 60} },
{ {70, 80}, {90, 100}, {110, 120} }
};

printf("Given 3Dimentional Array = int multi[2][3][2] = { { {155, 255}, {30, 40}, {50, 60} }, { {70, 80}, {90, 100}, {110, 120} } }\n");

/* Determining the sizes of Elements of multi-dimensional array */

printf("\nDetermining the sizes of Elements\n");

printf("The size of multi = %u\n", sizeof(multi));
printf("The size of multi[0] = %u\n", sizeof(multi[0]));
printf("The size of multi[0][0] = %u\n", sizeof(multi[0][0]));
printf("The size of multi[0][0][0] = %u\n", sizeof(multi[0][0][0]));

printf("\nPointer Location in Memory of Three-Dimentional Array in Hex(%x) use (%%d) if you want in decimal value \n");

/* Pointer Value in Memory */
printf("The value of (multi) = %x\n", multi);
printf("The value of (multi + 1) = %x\n", multi[0]);
printf("The value of &amp;multi[0][0] = %x\n", &amp;multi[0][0]);
printf("The value of &amp;multi[0][0][0] = %x\n", &amp;multi[0][0][0]);

//change %x above to %d to see the decimal value

printf("The 1st value of THREE-dimentional array\n");

printf("\nLet us get the 1st element of Multi-Dimentional Array\n");
printf("%d\n", multi[0][0][0]);
printf("%d\n", *multi[0][0]);
printf("%d\n", **multi[0]);
printf("%d\n", ***multi);

printf("\nThe 2nd value of THREE-Dimentional array\n");

printf("%d\n", multi[0][0][1]);
printf("%d\n", *(multi[0][0]+1));
printf("%d\n", *(*multi[0]+1));
printf("%d\n", *(**multi + 1));

int result = sum(&amp;multi[0][0][0], &amp;multi[0][0][0]);
printf("\nThe sum of the first element of array added to itself %d\n", result);

return 0;
}

int sum( int *p1, int *p2)
{
return *p1 + *p1;
}

/******

OUTPUT

*******/

Given 3Dimentional Array = int multi[2][3][2] = { { {155, 255}, {30, 40}, {50, 60} }, { {70, 80}, {90, 100}, {110, 120} } }

Determining the sizes of Elements
The size of multi = 48
The size of multi[0] = 24
The size of multi[0][0] = 8
The size of multi[0][0][0] = 4

Pointer Location in Memory of Three-Dimentional Array in Hex(4) use (%d) if you want in decimal value
The value of (multi) = 11fff8c
The value of (multi + 1) = 11fff8c
The value of &multi[0][0] = 11fff8c
The value of &multi[0][0][0] = 11fff8c
The 1st value of THREE-dimentional array

Let us get the 1st element of Multi-Dimentional Array
500
500
500
500

The 2nd value of THREE-Dimentional array
255
255
255
255

The sum of the first element of array added to itself 1000

THE 1000 IS 500 + 500 I added the first element of array by itself.

 

TAKENOTE: THEY HAVE THE SAME ADDRESS IN MEMORY EVEN THOUGH THEY ARE MULTIPLE ARRAYS.

Pointer Location in Memory of Three-Dimentional Array in Hex(4) use (%d) if you want in decimal value
The value of (multi) = 11fff8c
The value of (multi + 1) = 11fff8c
The value of &multi[0][0] = 11fff8c
The value of &multi[0][0][0] = 11fff8c

MEANING YOU CAN USE ANY DIFFERENT FORMAT WHEN IT COMES TO GETTING THE ADDRESS OF YOUR ARRAY ELEMENT IN ANY ARRAY TYPE SINGLE, TWO, OR THREE DIMENTIONAL ARRAY.

 

HERE ARE SOME POINTERS COMPLICATED DECLARATION ACCORDING TO K & R PAGE 90

int *f(); /* f: function returning pointer to int */
int (*pf)(); /* pf: pointer to function returning int */

char **argv
argv: pointer to char
int (*daytab)[13]
daytab: pointer to array[13] of int
int *daytab[13]
daytab: array[13] of pointer to int
void *comp()
comp: function returning pointer to void
void (*comp)()
comp: pointer to function returning void
char (*(*x())[])()
x: function returning pointer to array[] of
pointer to function returning char
char (*(*x[3])())[5]
x: array[3] of pointer to function returning
pointer to array[5] of char

THE END!

BY: ARMAN DE CASTRO

Advertisements
This entry was posted in C. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s