Global variable-sized arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Johnson

    #1

    Global variable-sized arrays

    I've got a program that has two functions, and both need to access an
    array. However, the size of the array is not even defined until I'm
    partially through the main() function. Here's a quick demonstration of
    what I need:

    #include <stdio.h>
    #include <stdlib.h>

    void fubar(int,int);

    // This is the array I want to make available to both functions
    int equation[count];

    int main()
    {
    int y, count = 0;
    while(scanf("%d ",&y) != EOF)
    count++;

    fubar(y,count);

    printf("%d\n",e quation[0]);
    }

    void fubar(int y, int count)
    {
    int i;
    for(i = 0; i < count; i++)
    equation[i] = y;
    }

    Effectively what I need is to be able to declare global variables within a
    function. Is that possible? Or else is there a workaround?
  • Jason Whitehurst

    #2
    Re: Global variable-sized arrays

    Chris Johnson wrote:[color=blue]
    > Effectively what I need is to be able to declare global variables
    > within a function. Is that possible? Or else is there a workaround?[/color]

    int *equation = NULL;

    int main(void) {
    ..
    ..
    ..
    equation = malloc(sizeof(i nt) * count);
    ..
    ..
    ..
    }

    --
    Jason Whitehurst


    Comment

    • Jack Klein

      #3
      Re: Global variable-sized arrays

      On Mon, 22 Nov 2004 22:09:55 -0500, "Jason Whitehurst"
      <jasonmac@cc.ga tech.edu> wrote in comp.lang.c:
      [color=blue]
      > Chris Johnson wrote:[color=green]
      > > Effectively what I need is to be able to declare global variables
      > > within a function. Is that possible? Or else is there a workaround?[/color]
      >[/color]

      #include <stdlib.h>
      [color=blue]
      > int *equation = NULL;[/color]

      Unnecessary for a pointer with static storage duration, which anything
      defined at file scope has. Will be initialized to NULL by default.
      [color=blue]
      > int main(void) {
      > .
      > .
      > .
      > equation = malloc(sizeof(i nt) * count);[/color]

      The recommend form by most CLC regulars is:

      equation = malloc(sizeof *equation * count);

      Saves editing and potential mistakes if it later turns out that the
      type of the array needs to be changed to long, or float, or anything
      other than int.

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      • Al Bowers

        #4
        Re: Global variable-sized arrays



        Chris Johnson wrote:[color=blue]
        > I've got a program that has two functions, and both need to access an
        > array. However, the size of the array is not even defined until I'm
        > partially through the main() function. Here's a quick demonstration of
        > what I need:
        >
        > #include <stdio.h>
        > #include <stdlib.h>
        >
        > void fubar(int,int);
        >
        > // This is the array I want to make available to both functions
        > int equation[count];
        >
        > int main()
        > {
        > int y, count = 0;
        > while(scanf("%d ",&y) != EOF)
        > count++;
        >
        > fubar(y,count);
        >
        > printf("%d\n",e quation[0]);
        > }
        >
        > void fubar(int y, int count)
        > {
        > int i;
        > for(i = 0; i < count; i++)
        > equation[i] = y;
        > }
        >
        > Effectively what I need is to be able to declare global variables within a
        > function. Is that possible? Or else is there a workaround?[/color]

        First of all you do not need a global varialbe to do this. You can
        use pointer arguments to modify variables.

        Since you are modifying the array length, you can use dynamic
        allocations to allocate the size as you need it.
        For example, you could prototype the function fubar to:

        int fubar(int **y, size_t *count, int value);

        where y is a pointer to a pointer to the dynamic allocated array.
        count is a pointer to a count of elements
        and value is the value you wish to insert in the array.
        The Function returns an int that will indicate success or failure
        of the allocation.

        Here is a working example using function realloc to do the allocations.

        #include <stdio.h>
        #include <stdlib.h>

        int AddArray(int **array,size_t *count,int value);

        int main(void)
        {
        int *equation = NULL, value;
        size_t i, count = 0;
        puts("Enter integer: (Enter 'q' to quit)");
        while(1 == scanf("%d",&val ue))
        if(!AddArray(&e quation, &count,value )) break;

        for(i = 0; i < count;i++)
        printf("equatio n[%u] = %d\n",i,equatio n[i]);
        free(equation);
        return 0;
        }

        int AddArray(int **array, size_t *count, int value)
        {
        int *tmp = realloc(*array, ((*count)+1)*(s izeof *tmp));
        if(tmp)
        {
        *array = tmp;
        (*array)[(*count)++] = value;
        return 1;
        }
        return 0;
        }


        --
        Al Bowers
        Tampa, Fl USA
        mailto: xabowers@myrapi dsys.com (remove the x to send email)
        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


        Comment

        • Jason Whitehurst

          #5
          Re: Global variable-sized arrays

          Jack Klein wrote:[color=blue]
          > Unnecessary for a pointer with static storage duration, which anything
          > defined at file scope has. Will be initialized to NULL by default.[/color]

          Of course it will. It seems he is learning though - I figure best to show
          him to just init to NULL. =)

          [color=blue]
          > equation = malloc(sizeof *equation * count);[/color]

          either way.

          --
          Jason Whitehurst


          Comment

          • Mark A. Odell

            #6
            Re: Global variable-sized arrays

            "Jason Whitehurst" <jasonmac@cc.ga tech.edu> wrote in
            news:cnuggl$b6q $1@news-int2.gatech.edu :
            [color=blue][color=green]
            >> Unnecessary for a pointer with static storage duration, which anything
            >> defined at file scope has. Will be initialized to NULL by default.[/color]
            >
            > Of course it will. It seems he is learning though - I figure best to
            > show him to just init to NULL. =)
            >
            >[color=green]
            >> equation = malloc(sizeof *equation * count);[/color]
            >
            > either way.[/color]

            Either the maintenance nightmare way or the self-documenting,
            automatically type correcting way?

            --
            - Mark ->
            --

            Comment

            Working...