Skip to content
Snippets Groups Projects
utils.c 611 B
Newer Older
Bruce Cowan's avatar
Bruce Cowan committed
/*
 * SPDX-FileCopyrightText: 2019 Bruce Cowan <bruce@bcowan.me.uk>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

Bruce Cowan's avatar
Bruce Cowan committed
#include <stdio.h>
#include <stdlib.h>

int *
get_ints (int         argc,
          char      **argv,
          int         n,
          const char *prompt)
{
    int *ret = malloc (sizeof (int) * n);

    if (argc != (n + 1))
    {
        for (int i = 0; i < n; i++)
        {
            printf ("%s: ", prompt);
            scanf ("%d", &ret[i]);
        }
    }
    else
    {
        for (int i = 0; i < n; i++)
            sscanf (argv[i + 1], "%d", &ret[i]);
    }
    
    return ret;
}