programmer99
programmer99

Reputation: 25

Can I have an array with indeterminate length in C?

so I am a beginner in c, and I am doing this program where a user input values in an array and if the value is 101 then the program is finished. the thing is I don't want the length of the array to be initialized, rather when the user enter 101 there would be the last value and the last place in the array. for example they entered 101 in the third place then the length of the array would be 4. I don't know if that possible.

#include <stdio.h>


int main(void) {

  int i,j;
  int a[i];

 for(i=0;i<10;i++)
 {
   printf("Enter Value Nummber %d:",i);
   scanf("%d",&a[i]);


   if 
     (a[i] == 101) break;
     else continue;


 }

Here I put the max length could be 10 but, if the user for example gives just 4 values and then 101 then it gives the rest of spots garbage values.

Upvotes: 0

Views: 608

Answers (3)

Raimund Kr&#228;mer
Raimund Kr&#228;mer

Reputation: 1309

I recommend having a look at malloc. It allows you to dynamically allocate memory and gives you a pointer to the start of the allocated piece of memory.

You can access an element with a pointer that points to the first element and then increase the pointer every time you add a value. Unlike other languages, C does not prevent you from accessing values outside of an array, so track the number of items added with a counter, and make sure to stay within your allocated memory (so save the length in a variable). You can then replace the for loop with an infinite loop, and break once the user inputs the value 101. When you reach the limit of your allocated memory, you can allocate a new array twice the size (so make sure to save the length in a variable) and copy the previous array there, then free the first array.

Upvotes: 1

"Can I have an array with indeterminate length?"

I don't want the length of the array to be initialized, rather when the user enter 101 there would be the last value and the last place in the array. For example they entered 101 in the third place then the length of the array would be 4. I don't know if that possible.

No, it is not possible.

int a[i];

You cannot write to an indeterminate array, as i is not initialized - it holds a garbage value.

Any such attempt is undefined behavior and doesn´t make much sense.

How do you want to store the value the user puts in to the assumed third element if there has not been space allocated for it?

Of course you can use VLAs or allocate and reallocate dynamic memory (as the other two answers suggest) but you can´t write data to memory which isn´t allocated for storing that values.

I do not understand your idea behind. Maybe you explain the bigger goal you want to achieve.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134366

In this case

  int a[i];

is called a variable length array, and you're using i unitialized, and that can invoke undefined behaviour. You need to have a defined value of i before you can use it.

That said, the cleaner approach will be (without using dynamic allocation and re-sizing)

  • Define a higher limit (#define MAXLEN 100) and use that to create an array, and initialize it to a value (say, 0).
  • Keep on asking user for the inputs, until (whichever is earlier)

    • You get a value 101
    • Counter reaches MAXLEN

Otherwise, if you're okay with using allocated memory (malloc()/ realloc() and family), then:

  • Define a starting size (#define INITSIZ 32)
  • Allocate memory for that size using malloc().
  • Ask user for inputs.
  • If you get an input value of 101, free() the allocated memory and exit.
  • Otherwise, once you hit the size, double the allocation using realloc() and continue to previous step.

Upvotes: 2

Related Questions