Thread: Is there such a thing as a undefined array?

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    1

    Is there such a thing as a undefined array?

    Hello
    I am trying to relearn C and am playing around with arrays. Is it possible to build an array of unknown size at compile time, but define it when I run my program?
    I wrote a program to sort up to 100 numbers, and I got to wondering how would you do that if you had more or for that matter less?
    I would think that it be possible since I would think that reading in a file would work on the same principle

    If there is such a beast can you just tell me what it is called?

    Thanks
    Fred

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You can use functions like malloc() to define a block of memory for use and gain a pointer to it......

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    85
    make a dynamic array. There is a lot of code examples out there on the net. Use your SEARCH
    friend to find DYNAMIC array code examples.
    Goodluck.
    DV007

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is it possible to build an array of unknown size at compile time,
    >but define it when I run my program?
    Yes, you simulate a dynamic array with pointers:
    Code:
    int *array, size;
    /* Get the size from the user */
    array = malloc ( size * sizeof *array );
    if ( array != NULL ) {
      /* All is well, work with the array */
    }
    else {
      /* Print an error and try to recover */
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well it looks like everyone told you the the same thing. Here is the deal, the function main is statically designed so that the os knows exactly how to start a process. Now, a function that main calls will dynamically build its memory usage. There is nothing wrong with doing this:

    Code:
    int myfunction(int size) {
          char str[size];
    
          //do other stuff
          return 1; //or whatever this function wants to return
    }
    BUT it is not okay for you to return the address of this dynamically created array:
    Code:
    //WRONG
    char *myfunction(int size) {
          char str[size];
          return str;
    }
    Since the returned string will be the address of a variable local to that function. Remember that the funcion will be deleting all of its local variables when it is done!

    Now here is some other cool things that you can do along similar lines:

    Code:
    int myfunction(int xsize, int ysize) {
          struct dynamic_struct {
                int x[xsize];
                int y[ysize];
          } array;
    
          //do stuff...
    
          return sizeof(array);    
    }
    Once again, if you want to have a function return a pointer to something then use malloc(). If the array isn't needed outside the array then you can do something like I just showed you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Makefile
    By pinkprincess in forum C Programming
    Replies: 3
    Last Post: 06-24-2007, 09:02 AM
  2. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM