944,028 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6202
  • C RSS
Mar 30th, 2005
0

Using a variable as an array's index?

Expand Post »
Hey all,

First post here and hello to everyone.

I've been studying C for a few months now, and everythnig has been making sense and I'm enjoying the challenge of learning to progeam.

Ther is one thing that keeps eluding my understanding of arrays, and that's using a variable in a for loop such as this:

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int array[10];
  6. int i;
  7.  
  8. for (i = 0; i < 10; ++i){
  9. array[i]=i;
  10.  
  11. printf("%d\n", array[i]);
  12. }
  13. return 0;
  14. }

No matter how I look at this, I can't seem to grasp the function of the variable i in this bit of the code, array[i]. I can see that the loop runs up to 9, that the value of i is being assigned to each element in the array as the loop runs, that the variable i is being used as the index for the array, but why is the i in the array[i] part?

Apologies if this is so basic, but it's driving me mad. Strange thing is, I find pointers to be easier to grasp tham this one thing. lol

Cheers.
Last edited by duner; Mar 30th, 2005 at 2:22 pm. Reason: code formatting
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
duner is offline Offline
2 posts
since Mar 2005
Mar 30th, 2005
0

Re: Using a variable as an array's index?

Sometimes you just have to look at something for a while before it clicks. Adding extra printf statments can sometimes help to see what is going on.
#include <stdio.h>

int main(void)
{
   int i, array[10];
   for ( i = 0; i < 10; ++i )
   {
      array[i] = i;
      printf("array[%d] = %d\n", i, array[i]);
   }
   return 0;
}

/* my output
array[0] = 0
array[1] = 1
array[2] = 2
array[3] = 3
array[4] = 4
array[5] = 5
array[6] = 6
array[7] = 7
array[8] = 8
array[9] = 9
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 30th, 2005
0

Re: Using a variable as an array's index?

Ahh, now I see it. I have a tendency to look too closely at things.

Thanks a million for that, it helps in the way you showed your example.

Cheers.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
duner is offline Offline
2 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Array cell contains the memory address of the next cell ????
Next Thread in C Forum Timeline: Need Help with an array problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC