Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 15th, 2013, 10:39 AM
 Bikal11
Registered User

Join Date: Mar 2013
Posts: 2
Time spent in forums: 41 m 57 sec
Reputation 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
March 15th, 2013, 11:10 AM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,974
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 55 m 27 sec
Reputation Power: 1283
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
March 15th, 2013, 12:35 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,868
Time spent in forums: 3 Months 1 Day 8 h 13 m 15 sec
Reputation Power: 2199
Quote:
 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
March 15th, 2013, 01:14 PM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,974
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 55 m 27 sec
Reputation Power: 1283
Quote:
 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
March 16th, 2013, 07:35 PM
 eramit2010
Contributing User

Join Date: Mar 2013
Location: India
Posts: 95
Time spent in forums: 2 Days 18 h 10 m
Reputation Power: 3
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
March 20th, 2013, 03:58 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,868
Time spent in forums: 3 Months 1 Day 8 h 13 m 15 sec
Reputation Power: 2199
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/s...elly/skelly.htm (Part 1) and at http://collaboration.cmc.ec.gc.ca/s...elly/skelly.htm (Part 2).

Share and enjoy!

Last edited by dwise1_aol : March 20th, 2013 at 04:03 PM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Please help me understand this program...