| | |
Using a variable as an array's index?
![]() |
•
•
Join Date: Mar 2005
Posts: 2
Reputation:
Solved Threads: 0
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:
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.
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> int main() { int array[10]; int i; for (i = 0; i < 10; ++i){ array[i]=i; printf("%d\n", array[i]); } return 0; }
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
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
![]() |
Similar Threads
- First Attempt at Arrays (Java)
- C++: compile error "subscripted value is neither array nor pointer" (C++)
- Print the parallel array index with the largest value? (C++)
- Array Index Problem in Quick Sort (C#)
- Determining an Array's Length in Pascal (Pascal and Delphi)
- filling variable with array from vb code. (HTML and CSS)
Other Threads in the C Forum
- Previous Thread: Array cell contains the memory address of the next cell ????
- Next Thread: Need Help with an array problem
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file floatingpointvalidation fork forloop frequency givemetehcodez grade gtkgcurlcompiling gtkwinlinux hacking highest histogram inches input intmain() iso kernel keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf opendocumentformat openwebfoundation owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault sequential single socket socketprograming socketprogramming stack standard string systemcall threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi







I have a tendency to look too closely at things.