 |
| C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

January 12th, 2004, 02:34 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
howzzat!!.. array in c
say a[10] is an array of integers,and say i is an integer counter.
to print the array elements we say
for (i=0;i<n;i++)
printf("%d",a[i]);
but what if i say
printf("%d",i[a]) ;
???????????????????????????????????????what is the result and why is it so?
|

January 12th, 2004, 04:49 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why don't you compile it to see the result yourself?
To be brief, the reason you can't do something like that is because "i" is an integer variable, NOT a pointer or array variable, so you can't access it via some index.
Also, "a" IS an array variable. Technically, "a" is the name of the array but acts internally as a pointer to the first index in the array. As such, the value of "a" is the address in memory where the array variable is stored. These memory locations are just numbers, so theoretically this value can be cast as an integer for use in an array index, but it doesn't make sense to do so. Ever.
Take care,
Nik
http://www.bigaction.org/
|

January 14th, 2004, 04:25 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
nik,
it did print the array right!!
i wonder!
|

February 5th, 2004, 10:41 PM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Let's say I have two decks of cards, it's ten cards in each deck. The first deck is called a, and the other is called b. The first card in each deck is called card number zero, then the last card ( number 10 ) is called card number 9. This is what we call zero based indexes. We start counting from zero, not from number one. So if i = 0 , then a[ i ], is the first card in deck a, and b[ i ] is the first card in deck b. If i = 5, then ( remember that this is zero based, so you have to add one to the number of the index( doing this wrong is often called off by one error ) ) a[ i ] is card number six in deck a, and b[ i ] is card number six in deck b. What you have to do in programming is first to point at the deck that you want a card from, and then say the number of the card. Saying the number first, and then the deck, is making caos. Point at the mountain, and then tell which path/route you wanna climb. It has no meaning, to tell about the path/route if the people that are listening to you, don't know wich mountain you are talking about. So saying i[ a ] has no meaning.
best regards
rune
|

February 8th, 2004, 07:35 AM
|
|
Registered User
|
|
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
More importantly, run the following loops. What is the output, and why is it so?
for (i=0;i<n;i++)
printf("%d",a[i]);
for (i=0;i<n;i++)
printf("%d",*(a+i));
"The process of preparing programs for a digital computer is especially attractive, not only because it can be economically and scientifically rewarding, but also because it can be an aesthetic experience much like composing poetry or music." - Donald Knuth
|

February 22nd, 2004, 11:53 PM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, this is obviously a trick question, since no one
would deliberately do such a thing (!)
However, K&R states "In evaluating a[ i ], C converts it to
*(a+i)." Now, you must believe that; then you can see that
i[ a ] is converted to *(i+a). In both cases, the quantity in
parentheses is a pointer + an integer (the same quantity,
regardless of how you write it). The trick question is designed
to confuse the student (or, maybe, make him think about the
really intimate relationship between pointers and array
indices).
Dave
Quote:
quote:Originally posted by kv_shan
say a[10] is an array of integers,and say i is an integer counter.
to print the array elements we say
for (i=0;i<n;i++)
printf("%d",a[i]);
but what if i say
printf("%d",i[a]) ;
???????????????????????????????????????what is the result and why is it so?
|
|

February 24th, 2004, 02:51 PM
|
|
Friend of Wrox
|
|
Join Date: Feb 2004
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Because the Compiler takes the array base and adds the offset to it to get the value,
In the case
a[i] --> It is direct take the array base of "a" and move to offset i within that.
i[a] --> In this it is not direct but the compiler takes it in the same manner as the above statement.
The think K&R statement is right, if u r not getting this answer.
Regards
Pradeep P
It is not how much we do,
but how much love we put in the doing.
-Mother Theresa
|
|
 |