Thread: How to declare a global variable in a function?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    How to declare a global variable in a function?

    Is there any way to declare a global variable in a local function instead declare it at the top?

    My situation is that:

    Code:
    struct data {
    int a;
    int b;
    }
    
    int main () {
    ...}
    
    int function () {
    // get an integer to variable "num" here  
    struct data info[num];
    }
    How can info[num] be declared as global variable?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It can't, since you want to dimension to be determined at run time. Define a pointer, either at file scope or prefixed with the "static" keyword, and then use malloc() to dynamically allocate memory.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should know the difference between declaration and definition.
    I think, most people (and you) say declaration and mean definition.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A definition is a type of declaration, Billy
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    A definition is not a type of declaration, it implicies a declaration.
    A program with declarations cannot work/compile/link, only with definitions.
    Therefore every program needs ever definitions and that's it, what he mean and i say.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BillyTKid
    A definition is not a type of declaration, it implicies a declaration.
    The C standard disagrees with you:
    Quote Originally Posted by C99 Clause 6.7 Paragraph 5
    A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
    • for an object, causes storage to be reserved for that object;
    • for a function, includes the function body;
    • for an enumeration constant or typedef name, is the (only) declaration of the identifier.
    That said, yes, in this case "definition" would have been more precise than "declaration".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global/Static function/variable slower to access?
    By JohnLeeroy in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2011, 11:56 PM
  2. Global variable problem (function related)
    By Ktulu in forum Windows Programming
    Replies: 3
    Last Post: 05-25-2007, 09:56 AM
  3. Replies: 2
    Last Post: 02-09-2006, 06:56 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. How to declare a global variable in Main()
    By vnrabbit in forum C Programming
    Replies: 2
    Last Post: 06-20-2002, 12:59 PM