Thread: get dimension of a table in c program from bash script

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    2

    get dimension of a table in c program from bash script

    Hi everybody,

    I am using a C program in a loop of a Bash script.

    In the C program there is a table whose dimensions should be updated at each index of the loop.
    Usually I declare the dimension by using define function as below:
    Code:
    #define N 25
    . However, in this case it will be time consuming, because I have to change N at each increment of the loop.

    How can I directly introduce the dimension N from the bash script to my C program.

    Thank you

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,035
    You likely will need to learn how to use dynamic memory management; but, this is just a guess based on the little information that you posted.
    If you mean to recompile the program after each change then you could just change a header with the define by using a bash script.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Mar 2021
    Posts
    2
    Sorry for not bein clear. I am some thing similar to:

    [COLOR=var(--highlight-color)]#!/bin/sh

    N=(1 2 3 4 5)

    cc -o test test.c -lm
    ./test

    [/COLOR]
    N is a dimension of tables in program test.c (below)


    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #define dimension N
    Thanks

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,126
    Don't recompile the C program for every invocation!
    Instead, pass the size as a command-line argument and dynamically allocate the memory.
    In the bash script, if you wanted a dimension of 100 you would call the program like this:
    Code:
    program_name 100
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define MAX_DIMENSION 1000
     
    void use_a(int *a, int n)
    {
        for (int i = 0; i < n; ++i) a[i] = i;
        for (int i = 0; i < n; ++i) printf("%d ", a[i]);
        putchar('\n');
    }
      
    int main(int argc, char **argv)
    {
        if (argc != 2)
        {
            fprintf(stderr, "Usage: %s DIMENSION\n", argv[0]);
            exit(EXIT_FAILURE);
        }
     
        int n = atoi(argv[1]);
     
        if (n < 1 || n > MAX_DIMENSION)
        {
            fprintf(stderr, "Error: bad dimension: %d\n", n);
            exit(EXIT_FAILURE);
        }
     
        int *a = malloc(n * sizeof *a);
        if (!a)
        {
            perror("Error: malloc");
            exit(EXIT_FAILURE);
        }
     
        use_a(a, n);
     
        free(a);
     
        return 0;
    }
    Example bash script:
    Code:
    #!/bin/bash
     
    N=(5 10 15 20)
     
    for n in ${N[*]}; do
        ./myprog $n
    done
    Output:
    Code:
    0 1 2 3 4 
    0 1 2 3 4 5 6 7 8 9 
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    Last edited by john.c; 03-09-2021 at 02:43 PM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bash script in c
    By programmerc in forum C Programming
    Replies: 1
    Last Post: 10-02-2014, 08:47 PM
  2. Awk and sed bash script help
    By Annonymous in forum Linux Programming
    Replies: 19
    Last Post: 05-10-2012, 12:40 AM
  3. ssh/bash script question
    By Overworked_PhD in forum Tech Board
    Replies: 2
    Last Post: 03-30-2009, 07:48 PM
  4. Bash Script Q
    By QuestionC in forum Tech Board
    Replies: 1
    Last Post: 04-19-2007, 10:16 AM

Tags for this Thread