Thread: define an array globally

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    2

    define an array globally

    I want to declare a global array and then use it in a few functions. When I want to assign any number to that array I face with this error:

    Code:
    a.c:11:6: error: expected expression before ] token
        n[]={1,2,3,4,5};
          ^

    The code is:
    Code:
    #include <stdio.h>
     
    int n[5];  // I need to define the array here to be global
    
    int main ()
    {
       n[]={1,2,3,4,5};
    
    
       for (int i = 0; i < 5; i++)
          printf("n[%d] = %d\n", i, n[i]);
     
       return 0;
    }
    Any help is appriciated. Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Arrays can't be assigned.

    They can only be initialised at the point of definition (i.e. line 3 in your code). After that, elements need to be assigned individually (e.g. in a loop) or values of memory locations need to be set (e.g. memset(), memcpy())
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,998
    You cannot assign to an array after it has been defined; you would assign to its elements. You can initialise the array, but that has to be done at the point of definition:
    Code:
    int n[5] = {1, 2, 3, 4, 5};
    One approach then is to forward declare the array as extern in a header file:
    Code:
    extern int n[5];
    then define it in a single source file.

    That said, are you sure you need a global array in the first place? If feasible, you should declare the array local to a function, e.g., the main function, then pass it around to other functions as needed via a pointer to its first element (and also providing its size).
    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

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    Why do you "need" to declare the array in the global scope. It is usually preferred to create variables in local functions and pass the variables to and from functions as parameters and return values.


    You can't use assignment for arrays except when you declare them.


    Jim

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    2
    Many Thanks to all of you.
    C really has it's limitations! Doing the same thig is just as easy as it can be in almost any other language but...
    Any way! Thanks I'll try to avoid this somehow.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,998
    Quote Originally Posted by khodam
    C really has it's limitations! Doing the same thig is just as easy as it can be in almost any other language but...
    C++ has the same "limitation", but then it has a shared heritage with C. C# and Java also have the same "limitation", and while inspired in part by C, they are further apart. I can think of several languages with don't have such a "limitation", but they are more on the dynamic typing side, so they don't really count.

    When programming in C, program in C, not some other programming language.
    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

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by khodam View Post
    C really has it's limitations! Doing the same thig is just as easy as it can be in almost any other language but...
    Oh, rubbish. All real-world programming languages have practical limitations, including whatever favourite language of yours that works as you suggest. Yes, there are programming languages that allow working with arrays in the way you describe, but they are significantly outnumbered by languages that do not. Supporting such a capability involves several trade-offs that aren't always desirable.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make my class globally accessible
    By baxy in forum C++ Programming
    Replies: 7
    Last Post: 08-08-2013, 10:13 PM
  2. #define array[8]
    By Ducky in forum C++ Programming
    Replies: 14
    Last Post: 05-17-2013, 12:25 PM
  3. Using a linked list globally and File I/O
    By Leftos in forum C Programming
    Replies: 46
    Last Post: 01-07-2008, 11:21 AM
  4. Why can't I use const globally?
    By keira in forum C Programming
    Replies: 8
    Last Post: 09-15-2007, 04:30 AM
  5. App.FormX.AllControls --> Globally Expose
    By willkoh in forum C# Programming
    Replies: 8
    Last Post: 07-31-2005, 07:54 AM

Tags for this Thread