March 15th, 2013, 09:39 AM
Please help me understand this program...
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=1, a=2 but what's the value of "a" only?
Please help me understand this...My exams are near and I really need to understand this one as it has already been asked!!
March 15th, 2013, 10:10 AM
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.
int* b[3 ] indicates a dynamic array of ints of size 3.
*b indicates the value in the array pointed to by the reference b at index 0.
March 15th, 2013, 11:35 AM
Actually, b is an array of three pointers to int. The array itself is not dynamic.
Originally Posted by bullet
March 15th, 2013, 12:14 PM
Oops you're right. I'm so used to dealing with dynamic arrays, I didn't notice the new missing.
Originally Posted by dwise1_aol
March 16th, 2013, 06:35 PM
1.so many * are pointers to pointers to pointers so on.
2.in first iteration
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.
March 20th, 2013, 02:58 PM
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.
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.