| | |
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 |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches infiniteloop intmain() iso keyboard km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi







I have a tendency to look too closely at things.