arrays between functions, dependent on local variables

This is a discussion on arrays between functions, dependent on local variables within the C Programming forums, part of the General Programming Boards category; Here's essentially what my problem is: In main, the program gets the user to enter two natural numbers, these are ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    arrays between functions, dependent on local variables

    Here's essentially what my problem is:

    In main, the program gets the user to enter two natural numbers, these are then passed to a function which creates an array with the larger of these two numbers as the maximum element, and does stuff to this array.
    In main, I need access to this array again in order to print elements of it, and manipulate it etc.

    e.g. if the user enters 5 and 7, then I need to create an array[7] in the function, do stuff to it, and then be able to use it in main again.

    The only way I would really know how to make the array exist in both functions is to make it as a global variable, but as it's dependant on local variables in main (what the user inputs), I'm confused as to how I can make it so that I can use the array in both functions...

    Or is there a way of changing the size of an array? So I could declare it globally, then change its size to whatever I need once the numbers have been entered.

    Is there a way to do this?
    Cheers for any help

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Dynamically allocate the array according to the exact size you want, using malloc() (include stdlib.h).

    Change the return on the function, so it's going to return the char * (the address of the new array, which can be it's name). You don't want to lose that address

    When you are done with it, free(arrayname), to stop any memory "leaking" out. Always a great idea to follow.

    Since you're new to this, I highly recommend you click on the C Tutorial tab, and go to that section and read it - and take notes.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by usernamer View Post
    e.g. if the user enters 5 and 7, then I need to create an array[7] in the function, do stuff to it, and then be able to use it in main again.
    As Adak mentioned, using malloc is called for here. Keep in mind that each function that manipulates your array must have some way to know its length. The two basic approaches are to end the array using a special marker of some kind (the approach used by strcpy, for example), or to pass the length of the array along with the array itself. Here is an example of the second approach:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // create array with length arrlen
    void arrcreate(double **arr, int arrlen);
    
    // set all elements to x
    void arrsetall(double *arr, int arrlen, double x);
    
    // release memory
    void arrdestroy(double **arr);
    
    int main()
    {
        double *a = NULL;
        arrcreate(&a, 10000);
        arrsetall(a, 10000, 1.234);
        arrdestroy(&a);        
        exit(EXIT_SUCCESS);
    }
    
    void arrcreate(double **arr, int arrlen)
    {
        if (*arr != NULL) {
            printf("arrcreate: argument 1 must be a NULL pointer\n");
            exit(EXIT_FAILURE);
        }
        *arr = malloc(arrlen * sizeof(**arr));
        if (*arr == NULL) {
            printf("arrcreate: allocation error\n");
            exit(EXIT_FAILURE);
        }
    }
    
    void arrsetall(double *arr, int arrlen, double x)
    {
        for (int i=0; i < arrlen; i++)
            arr[i] = x;
    }
    
    void arrdestroy(double **arr)
    {
        free(*arr);
        *arr = NULL;
    }

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Thanks, think I've got it now.

    Problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-02-2009, 07:47 AM
  2. local variables performance
    By KIBO in forum C++ Programming
    Replies: 10
    Last Post: 04-17-2009, 04:41 PM
  3. Globla and local variables
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 11-19-2006, 04:56 AM
  4. local variables
    By Gil22 in forum C++ Programming
    Replies: 14
    Last Post: 04-09-2003, 09:13 PM
  5. local variables
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 03-20-2002, 01:55 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21