Confused with array size

This is a discussion on Confused with array size within the C Programming forums, part of the General Programming Boards category; Hi, I'm starting to get confused here..if I want an array to hold 2 values, then should I write int ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    30

    Confused with array size

    Hi,

    I'm starting to get confused here..if I want an array to hold 2 values, then should I write int values[1] or int values [2] ? I'm confused because of the 0 index.

    Also, when I wish to have an array of string, then is it just char values[n][20] (where 20 is the number of maximum characters and n is how many strings I have in the array) ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,771
    You should write:
    Code:
    int values[2];
    But if you want to access those elements of the array, it would be values[0] and values[1]. values[2] does not exist.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Declare with the number of elements you want. If you declare n elements, then you have elements ranging from index 0 to n-1, inclusive.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    Quote Originally Posted by desmond5 View Post
    Hi,

    I'm starting to get confused here..if I want an array to hold 2 values, then should I write int values[1] or int values [2] ? I'm confused because of the 0 index.
    As everyone mentions, the syntax is array[length_of_array] and then you have length_of_array-1 elements because they count from 0.

    Also, when I wish to have an array of string, then is it just char values[n][20] (where 20 is the number of maximum characters and n is how many strings I have in the array) ?
    The syntax would be, I believe, char values[num_of_string][length_of_strings];
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's easier I believe, to think of a 2d array as being Array[rows][columns]. Internally, the compiler handles this quite differently, but it can be thought of thematically, as rows and columns, and was designed with that in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. size of array
    By goran00 in forum C Programming
    Replies: 38
    Last Post: 04-02-2008, 09:57 AM
  3. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  4. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21