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

AI Thread Summary
Declaring an array outside of the main function and initializing it within main is not valid in C, as shown in the example where the array is incorrectly assigned values. The correct way to initialize an array in C is either to do it at the point of declaration or within the main function itself, but not with an assignment statement. The discussion also touches on the differences between C and C++, noting that while C is a subset of C++, the two languages have distinct features and rules. Additionally, the use of initializer lists is specific to variable declarations in C, while C++11 allows for more flexibility with anonymous arrays. Understanding these differences is crucial for effective programming in both languages.
nobahar
Messages
482
Reaction score
2
Hello!

I am sure I am again missing something obvious:
I tried to declare the array and assing value within main() as follows:
Code:
#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:
#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:
#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!
 
Technology news on Phys.org
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:
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:
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.
 
nobahar said:
does C++ apply to C?

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.
 
jedishrfu said:
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:
float *xxx = { 1.0, 2.0, 3.0 };

main() {

  xxx = { 4.0,5.0,6.0};

}
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.
 
AlephZero said:
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.

Thanks for the clarification, my C/C++ experience is from the 1980s and has been muddled together with my present java knowledge.
 
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...
 
nobahar said:
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...

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
 
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.
 
  • #10
phinds said:
one thing to note is that any C++ compiler will automatically compile C code because C is a subset of C++.

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.
 
Back
Top