Thread: Arrays???

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Arrays???

    I am learning arrays right now and I am being troubled! I understand that they are like more variables into one! If I am wrong, please, please, correct me!

    so instead of doing:
    int no1
    int no2
    int no3
    etc

    You could do
    int no[10]
    or in this case,
    int no[3]!

    But I seen that many statements have arrays to them, like in a program I seen
    ++ndigit[c-'0'];

    All I want to know is the basics, what are they, what are they used for, why do we want to use them, anything!

    printf ("Thank You Very Much\n");

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Arrays are basically used to declare and allocate lots of memory at once. If you wanted to declare 100 ints, how would you go about doing it?

    int x1, x2, x3.... would be bad. That's a horrible way to declare that much memory. You would be better to do something like:

    Code:
    int arr[100];
    This allocates them all at once. You now have 100 variables all known as arr. You access them via an index value. The first variable of the 100 is arr[0]. The next is arr[1], and so on until you get to arr[99]. arr[100] does not exist. Remember that. Arrays are declared with the amount of elements you want. Indexing them, however, for operations of any kind require you to start at 0.

    The important thing to realize is that arr[0] is just a regular int in this case. You can do anything you want, just like you can do to any other int. Don't focus on syntax at this point that appears complicated. Start slow.

    Write a program that has an array of 10 ints. Assign them values. Print out the array. Once you've done that, write a program that does the exact same, but instead of assigning them values in the program, have the program ask the user for the numbers. You can keep going like that.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    the c-'0' is a trick to convert a character to an integer but doesn't work on all characters so watch out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM