-5

I am beginner in C. I want to make an array whose size will be taken from user from scanf function because an array may have any size which is not known when program starts.

And how can I input array element as well:

I will like to have my program output as:

Please enter the number of array element: 4 Enter the elements: 12 43 5 6 The elements you entered are: 12 43 5 6

Is it possible to do this? How can I make my output like this?

3 Answers3

4

Yes it is very possible. It is called dynamic memory allocation. What you would do is create a pointer and then allocate it later.

int *array;
int num_elements;
//Get number of elements here
array = (int *)malloc(sizeof(int) * num_elements);
if(!array){ //Good practice to check if the allocation worked
    printf("Allocating %d bytes failed\n", (int)sizeof(int) * num_elements);
    return -1;
}
//Use the array are normal
free(array); // don't forget to free allocated memory

Pointer access works just like a static array i.e. array[0] = whatever

edit:

Don't forget you should include stdlib.h when using malloc()

3

Dynamic memory work well to your purpose, however as mention earlier by Gopi, C99 allow you to directly use the stack.

Here is another solution using stack instead of heap memory:

    #include <stdio.h>

    int     main(void)
    {
      int   nb;

      scanf("%d", &nb);
      printf("%d\n", nb);
      // declaration
      char  my_tab[nb];
      if  (nb > 2)
        {
          // I use my table as I want...
          my_tab[0] = 'a';
          my_tab[1] = 0;
          printf("%s\n", my_tab);
        }
      return (0);
    }

I hope this will help you understand better, the different kind of memory allocation.

Bernard Jesop
  • 747
  • 6
  • 11
0

A simple way of doing this with only basic knowledge might be to set the user input as a variable and then use that when you describe the size of the array:

#include <stdio.h>

int main(void)
{
    int arraysize, i;

    printf("Please input an array size.\n");
    scanf("%d", &arraysize); //Will set the user input and use it as array size
    getchar(); //Has user hit enter to continue
    i = arraysize; //This is for sheer convenience in the for() loop
    int array[i]; //This creates an array of the size inputted by the user
    printf("Please input %d numbers to fill the array.\n", i); 
    for(i = 0; i<arraysize; i++) //Has the user put in a number for every space in the array
    {
         scanf("%d", &array[i]); //The i coordinate updates with the i++
         getchar();

    }
    printf("Your numbers were: \n");
    for(i = 0; i<arraysize; i++) //Same thing as the previous for() loop
    {                            //except we are printing the numbers in the table
         printf("| %d |", array[i]);

    }
}

The output looks like:

[PROGRAM BEGINS]
Please input an array size.
5
Please input 5 numbers to fill the array.
1 2 33 4 5
Your numbers were:
| 1 || 2 || 33 || 4 || 5 |
[PROGRAM ENDS]

Hope that helps!

Joseph Farah
  • 2,463
  • 2
  • 25
  • 36