Thread: way around variable array size probs?

  1. #1
    Registered User
    Join Date
    Sep 2002

    way around variable array size probs?

    I know now that you can't define an array with the size determined at runtime. I'm trying to think of a useful work around that I could incoperate into a header file.
    I'd like to create a header file that does a lot of the leg work of working around the problem of variable size arrays ( stop me if this isn't possible, I'm new ).

    int i;
    int *ptr = (int*) calloc ( 100, sizeof (int));
    if (ptr == NULL)
    printf("Calloc failed!");
    for (i=0;((getchar () != 'x') == 1);i++) {
    ptr[i] = getchar ();

    At this point in any program I could have put into this block of memory ( ptr ) upto 100 integers. Firstly any ideas on how to shrink this into memory space into only the size I need it ( say I put in 5 numbers then have the array only holding 5 numbers and not 95 other "0"s as well).
    From that could I produce a header file which could make this into a command I call as if you can enter variable size arrays.

    I don't think I got my point arross well there, sorry about that. I'm not looking for code here. Just a shove in the right direction if its possible and to be told if its not.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >>I'd like to create a header file that does a lot of the leg work
    Header files are really only supposed to hold declarations etc, not perform actual work. (I am presuming you are talking about .h files here).

    To solve your problem, there are a number of options, including using realloc() or a list of some sort (link list, binary tree etc).

    >>ptr[i] = getchar ();
    What kind of int are you expecting to get when doing this? You do realise that if the user enters 5, your array will get an ASCII 5, which is 0x35. And if they enter 55, you'll still only get 0x35, as the getchar() function only gets one char.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Ahhh, I see the difference. I wasn't sure about the protocol surrounding .h files etc ( I'm only learning for something to do when I've free time, I'm a chemistry undergrad ). Thanks, point taken as well hammer. I'll make the input system more flexible.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Do any of your more experienced coders write your own work arounds common problems you encounter. Something you've coded once and saved so you don't have to re-write lots of stuff everytime?

    If so what of things have you done it for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 12:02 PM
  5. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM
Website Security Test