How to declare global arrays of unknown length?

This is a discussion on How to declare global arrays of unknown length? within the C Programming forums, part of the General Programming Boards category; I want to declare a global array of length p, which will be determined by the main function depending on ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    How to declare global arrays of unknown length?

    I want to declare a global array of length p, which will be determined by the main function depending on the particular case. Is this possible? When I process it, the complier says:

    variable-size type declared outside of any function

    Any way round this?

  2. #2
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    I guess you could make an array with malloc and then realloc it to get the lenght you wish... (not sure if it's a good way though)
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Global variables are those variables which will get initialized during compilation itself. Therefore you cannot dynamically allocate memory for global arrays. I think.
    I dont know why you need this. Can you please tell me why you need this (In fact I never heard of question like this).
    Saravanan.T.S.
    Beginner.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    reply

    I have a maths problem to solve and it consists of two parts, 1) calculating a list of prime numbers which I am storing in an array, 2) using those prime numbers in a recursion formula with some starting values.

    So instead of having to send an array of prime numbers of unknown length to a function, I have declared a global array to store them in, which my function to do the recursion can read.

    Does that make sense?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file of unknown length
    By the bassinvader in forum C Programming
    Replies: 2
    Last Post: 07-12-2006, 03:06 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. How to make unknown size arrays?
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 09-08-2003, 12:08 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. turning strings of unknown length into arrays
    By hankspears in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:16 PM

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