Thread: User input variable to global array help

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    User input variable to global array help

    I'm fairly new to programming so bear with me here please.

    I am trying to create a global array with user-defined dimensions.

    the code is:

    Code:
    int matr_size()
    {
      int x = 0;
    
    
      printf("Please enter the number of nodes: ");
      scanf( "%d", &x);
      printf("There are %d nodes in this simulation.", x);
      getchar();
      return x;
    }
    
    
    const int nodes = matr_size();
    
    
    float A_inv[nodes - 1][nodes - 1];
    A_inv[][] is then filled out in a function called matinv() and used later in main()

    I am getting errors:

    pyrol1.c:57:22: error: array bound is not an integer constant before ‘]’ token
    pyrol1.c:57:33: error: array bound is not an integer constant before ‘]’ token
    pyrol1.c: In function ‘int main()’:
    pyrol1.c:159:18: error: ‘A_inv’ was not declared in this scope
    pyrol1.c: In function ‘void matinv()’:
    pyrol1.c:205:11: error: ‘A_inv’ was not declared in this scope
    pyrol1.c:287:11: error: ‘A_inv’ was not declared in this scope



    I'm pretty sure I read that an array cannot be defined by a variable in C so I assume that is the issue, but I'm not sure how else to do it. Previously the size was defined by #define NODES and it worked fine but I need this user input. Any advice?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Any advice?
    First why must it be a global variable. Learn to use local variables and learn to properly pass these variables into your functions as parameters.

    Second use dynamic memory to allocate the memory.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    It is a global array because I'm pretty sure you cannot pass an array into a function. I'm still trying to learn the ins and outs of pointers, once I understand them better I imagine it won't need to be a global array anymore.

    When you say dynamic memory, are you talking about malloc? Could you possibly give me an example that might illustrate what you mean?

    Thanks

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It is a global array because I'm pretty sure you cannot pass an array into a function.
    Sure you can.

    When you say dynamic memory, are you talking about malloc?
    Yes, malloc/free.

    Could you possibly give me an example that might illustrate what you mean?
    I suggest you give your favorite search engine a try. You'll be amazed at how much information is but a couple clicks away.

    Here is one such link that may be of help. A tutorial on Pointers in C.

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    I have indeed tried to use said favorite search engine. Unfortunately I'm struggling to come up with the correct phasing of my problem to get useful results. That is why I decided to post on this forum.

    To clarify, are you saying my approach is wrong in the first place and that I should
    1. allocate the memory for the array dynamically
    2. declare the array in main
    3. and then pass the array to the function somehow?

    Also, what is wrong with having a global variable?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    To clarify, are you saying my approach is wrong in the first place
    Yes. When using a global array the size of the array must be a compile time constant (using #define).


    1. allocate the memory for the array dynamically
    Yes, use malloc to allocate the memory after you determine the size, then use free to free the memory when your finished with the array.

    2. declare the array in main
    In this case yes.

    3. and then pass the array to the function somehow?
    Yes, there is a link in the link I provided that illustrates how to pass arrays to functions.

    Also, what is wrong with having a global variable?
    In most cases using global variables is considered a bad practice because a global variable can be modified by any function at any time. In a small program this is not a big deal, but as your programs grow you'll find using global variables without good reasons makes your program brittle and easily broken. It'll make troubleshooting the program much harder because you'll have to look at every line of code to see where the modifications are occurring. With larger programs, you won't always be able to remember where exactly, and why exactly you are modifying that global. But by passing the variables via parameters you narrow down where the variable could be modified to functions that you pass the variable into.

    One of the goal of functions is to be able to test that the function preforms as desired. You should be able to test your functions outside your main program by creating a driver function to call the function with the desired values. When using global variables it is much much harder to do these tests because they rely on some external variable.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd results on user input array
    By deadrabbit in forum C Programming
    Replies: 4
    Last Post: 08-15-2011, 07:59 PM
  2. Replies: 2
    Last Post: 02-09-2006, 06:56 AM
  3. Choosing a variable based on user text input.
    By Compiling... in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2005, 01:21 AM
  4. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  5. array and user input
    By enlinux in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 02:08 AM

Tags for this Thread