1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
2
Rep Power
0

#include <stdio.h>
#include <conio.h>
void main()
{
int k;
int a[]={1,2,3};
int *b[3];
int **c[3];
int ***d[3];
int ****e[3];
int *****f[3];

for (k=0;k<3;k++)
{
b[k]=a+k;
c[k]=b+k;
d[k]=c+k;
e[k]=d+k;
f[k]=e+k;
}
for (k=0;k<3;k++)
{
printf("%3d",*b[k]);
printf("%3d",**c[k]);
printf("%3d",***d[k]);
printf("%3d",****e[k]);
printf("%3d\n",*****f[k]);
}

}

So, it prints this:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3

But HOW?? I'm a beginner at C, so here are the questions that are troubling me:

1) What is the significance of so many ***. I can understand the program when there's only one * obviously...but what does it mean with multiple *s?

2) What is the value of a,b,c,d and e in the first for loop and how? We have the value of a[0]=1, a[1]=2 but what's the value of "a" only?

2. In the declarations, the * indicate you are declaring pointers. In this case, the pointers are going to be used to refer to dynamic arrays.

In the print statements, the * indicates the dereferenced value of the pointer at that location.

So

int* b[3 ] indicates a dynamic array of ints of size 3.

*b[0] indicates the value in the array pointed to by the reference b at index 0.
3. Originally Posted by bullet
int* b[3 ] indicates a dynamic array of ints of size 3.
Actually, b is an array of three pointers to int. The array itself is not dynamic.
4. Originally Posted by dwise1_aol
Actually, b is an array of three pointers to int. The array itself is not dynamic.
Oops you're right. I'm so used to dealing with dynamic arrays, I didn't notice the new missing.
5. 1.so many * are pointers to pointers to pointers so on.

2.in first iteration
b[0]=&a[0]
c[0]=&b[0]
d[0]=&c[0]
e[0]=&d[0]
f[0]=&e[0]

b[1]=&a[1]
c[1]=&b[1]
d[1]=&c[1]
e[1]=&d[1]
f[1]=&e[1]

same for 3rd iteration.

so in 2nd for loop u r getting same 1 1 1 1 1 in first iteration.
2 2 2 2 2 in 2nd iteration and 3 3 3 3 3 in 3rd iteration.
6. This exercise in multiple levels of indirection (ie, pointer to a pointer to a pointer ... ) had reminded me of an excellent pair of articles written many years ago in the C Users Journal. I just found a copy in my files:
Pointer Power in C and C++, Parts 1 and 2 by Christopher Skelly, The C Users Journal, Feb and Mar 1993.

If your college/university library has that magazine in its periodicals section, then burn a copy for yourself. If not, then I found it on-line in the ACM Digital Library:
Part 1 -- http://dl.acm.org/citation.cfm?id=159671
Part 2 -- http://dl.acm.org/citation.cfm?id=177856.177862
I don't know what the requirements are for downloading the articles, but they don't look like they're free.

PS
Google on "Pointer Power in C and C++" "Christopher Skelly" for other options. I also found the articles re-posted at http://collaboration.cmc.ec.gc.ca/sc...lly/skelly.htm (Part 1) and at http://collaboration.cmc.ec.gc.ca/sc...lly/skelly.htm (Part 2).

Share and enjoy!
Last edited by dwise1_aol; March 20th, 2013 at 03:03 PM.