I'm not sure what the technical term for this is, so that's why I left the title as general as possible. So, I'm trying to write a program(in C) that will basically allow me to "customize" the size of an already defined array. This would be something done at the beginning of the program so all further calculations would be done with this special array.

The general idea is this:

An array is any size, say 5000 indexes. If I input a 500 via some function, then my array will have 4500 null spaces hence forth and 500 valid spaces with which to plug numbers into later in the program.

Is such a thing possible? If not that exact thing, then perhaps something similar? Or something different altogether that would still allow me to "customize" how many values are actually valid in an array based on what's entered?

Thanks in advance if I get any answers. I'd appreciate it if I didn't just get "here ya go!" code snippets, as this is an assignment that I'd like to try and mostly work out for myself. A friendly nudge in the right direction wouldn't hurt, though:icon_cheesygrin:

Just to clarify, as it may not have been made clear, but there aren't any other parts of my code that I'm having issues with. Using a pre-defined array of any size and doing various comparison tests/what have you isn't an issue.

Edited 5 Years Ago by Wheaties: n/a

I'm not sure if this is what you're leading towards, but you can always allocate memory during execution with malloc and a given size.

This isn't something that was covered in my class, so it would definitely be rough learning grounds. Would allocating memory elsewhere allow for a smaller array to work with? Using my previous example, allocating memory equal to the 4500 "spaces" in an array of size 5000 elsewhere so that only the first 500 spaces are what's used is what you're talking about?

Sorry for the onslaught of questions.

It's fine. I'm learning, too, so I'll try to answer what I can.

Is the size 5000 mandatory, or are you just looking for a way to create a smaller array that isn't predefined?

You can always specify the size when you allocate.

Check this out.

I know you'd rather not check out code, but since it wasn't covered in class, getting an understanding of it is fine. Here's hoping it helps out.

An array is any size, say 5000 indexes. If I input a 500 via some function, then my array will have 4500 null spaces hence forth and 500 valid spaces with which to plug numbers into later in the program.

Is such a thing possible?

Of course it is. Just because you specified the total size as 5000 does not mean you need to use all of it. Just use the value entered to limit your size, exactly the same as you would use 5000 to make sure you don't go beyond the array boundaries.

This article has been dead for over six months. Start a new discussion instead.