Results 1 to 7 of 7

Thread: Array with 0 index?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Texas
    Posts
    140

    Array with 0 index?

    I have an array tha has the number of indexes as a variable. It is possible that the variable could be 0 (arrExam[0]). What would this do?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    array indices always start with 0. ar[0] would access the first element in the array.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Texas
    Posts
    140
    What I meant was in the declaration of the array. If I declared it with 0 subsections...

  4. #4
    jim mcnamara
    Guest
    int ar[0];

    is the same thing as

    int ar;

    Both are just fine.

    If you want variable length arrays you adoing the hard way.
    Just use pointers -

    int *ar; -- this lets you have any number of elements.

    Code:
    int *ar, *ar_base;
    
    ar=(*int) malloc(100);
    ar_base = ar;
    ar_base is the first element, you can get any other element by addition ie., element # 5 is: ar = ar_base + 5;

  5. #5
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Or you could do:

    PHP Code:
    intar// Lets you have any number of ints

    ar = new int[100]; // 100 ints, you can use a variable in place of
                       // 100 if you don't know how many you'll need

    delete[] ar// Frees up memory at the end of program 
    The to get the 5th element, just do:

    PHP Code:
    cout << ar[4] << endl// Just like normal arrays 
    Alcohol & calculus don't mix.
    Never drink & derive.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by jim mcnamara
    int ar[0];
    The smallest array you can allocate is 1
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    Use if statement to allocate a single int and then in the delete use if statement again to deallocate that single int!
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width