Thread: Quick Array question

  1. #1
    Registered User
    Join Date
    Sep 2008

    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
    You don't.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Toronto, Canada
    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
    Join Date
    Oct 2007
    Inside my computer
    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.

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, 07:33 AM
  3. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05: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, 06:27 PM