Using a variable as an array's index?

Reply

Join Date: Mar 2005
Posts: 2
Reputation: duner is an unknown quantity at this point 
Solved Threads: 0
duner duner is offline Offline
Newbie Poster

Using a variable as an array's index?

 
0
  #1
Mar 30th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #2
Mar 30th, 2005
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
*/
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 2
Reputation: duner is an unknown quantity at this point 
Solved Threads: 0
duner duner is offline Offline
Newbie Poster

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

 
0
  #3
Mar 30th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC