Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming: array declared before main() and initialised in main()

  1. Dec 21, 2013 #1
    Hello!

    I am sure I am again missing something obvious:
    I tried to declare the array and assing value within main() as follows:
    Code (Text):
    #include <stdio.h>

    float array_1[10];

    int main( void )
    {
        array_1[10] = { 1,1,2,3,4,5,6,7,8,9 };
       
       ...other stuff...

        return 0;
    }
    I can declare and initialise together before main():
    Code (Text):
    #include <stdio.h>

    float array_1[10] = { 1,1,2,3,4,5,6,7,8,9 };

    int main( void )
    {
       
       ...other stuff...

        return 0;
    }
    and within main():
    Code (Text):
    #include <stdio.h>

    int main( void )
    {
        float array_1[10] = { 1,1,2,3,4,5,6,7,8,9 };
       
       ...other stuff...

        return 0;
    }
    Is this an actual "restriction" or am I doing something wrong? The textbook I am working through gives me no guidance on this.

    Thanks in advance!
     
  2. jcsd
  3. Dec 21, 2013 #2

    jedishrfu

    Staff: Mentor

    You could have defined it as a float pointer and then did the one assignment in your main using only the name and without the square bracket index stuff.

    Code (Text):


    float *xxx = { 1.0, 2.0, 3.0 };

    main() {

      xxx = { 4.0,5.0,6.0};

    }

     
    Also in your example, you've actually created a new array in the main context overriding the globally defined one.

    Here's a tutorial on this stuff that may help:

    http://www.cplusplus.com/doc/tutorial/arrays/
     
    Last edited: Dec 21, 2013
  4. Dec 21, 2013 #3
    I was confused because with single variables I can declare and initialise the variable before main() and then change the value in main() using the assign function = . I can also declare the variable outside main() and assign a value in main(); I figured arrays would be the same since they are "collections of variables", but apparently not.
    Thanks for the link, I have a textbook but it seems kind-of neglective on some points; does C++ apply to C? I know they are related, but I am new to C and I have never used C++.

    Thanks for the help jedishrfu.
     
  5. Dec 21, 2013 #4

    phinds

    User Avatar
    Gold Member
    2016 Award

    C++ is an object oriented language that uses the basic syntax of C, but it would be a huge mistake to think that if you know C then learning C++ is trivial. The OO stuff makes it a whole different ball game.

    EDIT: one thing to note is that any C++ compiler will automatically compile C code because C is a subset of C++. This is possible because C++ compilers, unlike Java for example, do not REQUIRE that everything be OO, they just ALLOW for OO programming.
     
  6. Dec 21, 2013 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    That is not standard C, and was not standard C++ until the latest version of the standard (C++11).

    In C, the { 1.0, 2.0, 3.0 } notation is an initializer list which can only be used when you declare a variable. C++11 has extended the notation to be an "anonymous array", i.e. an array of values that doesn't have a variable name, which you can use "anywhere" in similar ways to scalar constants like 42 or 3.0.
     
  7. Dec 21, 2013 #6

    jedishrfu

    Staff: Mentor

    Thanks for the clarification, my C/C++ experience is from the 1980s and has been muddled together with my present java knowledge.
     
  8. Dec 23, 2013 #7
    Thanks for the responses everyone! I was planning on familiarising myself with C then moving onto C++ - the textbook assures me this is a good move.....
     
  9. Dec 23, 2013 #8
    If you plan to use C extensively as well as C++, that makes sense. If your goal is C++ and you have been convinced that C is an important stepping stone, note that Bjarne Stroustrup strongly disagrees and has often spoken against this assertion.

    http://www.stroustrup.com/bs_faq.html#prerequisite

    For another example, check this interview, searching for the answer beginning "No. C isn't":

    http://electronicdesign.com/dev-tools/interview-bjarne-stroustrup-discusses-c
     
  10. Dec 26, 2013 #9
    Thanks for the links Integrand; I'll finish this textbook on C because I am half-way through and I have put in the effort already! I bought another textbook on C++ to start afterwards, one good thing is that C has eased me into this type-of programming.
     
  11. Dec 29, 2013 #10
    No, that is not true. For example, new and virtual are keywords in C++ that are not keywords in C. so any C code that has something named new or virtual won't be accepted by a C++ compiler.

    There are some more subtle differences, too. So to convert any non-trivial piece of C code into C++ is by no means automatic.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C programming: array declared before main() and initialised in main()
Loading...