Quick Array question

This is a discussion on Quick Array question within the C Programming forums, part of the General Programming Boards category; How do you decalare an array of an unknown value. basicly i needa scan a file with a bunch of ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Quick Array question

    How do you decalare an array of an unknown value.

    basicly i needa scan a file with a bunch of numbers in them and store em into an array. but i dont know how many numbers will be in the file could be 3 5 7 200,000 .

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Code:
    int *array;
    
    array = malloc(number_of_elements * sizeof(int));
    if (NULL == array) {
         printf("Failure to allocate memory for %d elements\n", number_of_elements);
        exit(0); }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Just a thought, but avoid putting exit in your generic code.
    Exit should really only be used in very specific situations and usually, such a situation does not occur, if for example, you fail to do something.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  2. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 06:33 AM
  3. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 04:39 AM
  4. Memory Address of Array Question
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 09:58 AM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

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