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

In summary, C++ is an object oriented language that uses the basic syntax of C, but it is not a direct subset of C. There are significant differences and it is not recommended to assume that if you know C, you know C++ automatically. It is important to learn C++ specifically, rather than just assuming it will be easy if you know C.
  • #1
nobahar
497
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
  • #2
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:
  • #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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #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...
 
  • #8
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
 
  • #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.
 
  • #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.
 

1. Why do arrays need to be declared before main() in C programming?

Arrays in C programming need to be declared before main() because they are allocated memory at compile time. This means that the size of the array must be known before the program starts executing. Declaring the array before main() allows the compiler to reserve the necessary amount of memory for the array.

2. Can arrays be initialised in main() in C programming?

Yes, arrays can be initialised in main() in C programming. Initialising an array means assigning values to the elements of the array when it is declared. This can be done in main() by using an assignment statement or a for loop to assign values to each element.

3. What happens if an array is not declared before main() in C programming?

If an array is not declared before main() in C programming, the program will not compile. This is because the compiler needs to know the size of the array before it can allocate memory for it. Without a declaration, the compiler will not be able to reserve the necessary memory for the array.

4. Is it possible to declare an array and initialise it later in C programming?

Yes, it is possible to declare an array and initialise it later in C programming. This can be done by using a separate assignment statement or for loop after the array has been declared. However, it is considered good practice to initialise arrays at the time of declaration.

5. Can arrays be declared and initialised multiple times in C programming?

Yes, arrays can be declared and initialised multiple times in C programming. This can be useful if you need to change the values of the array throughout the program. However, it is important to note that declaring and initialising an array multiple times can impact the performance of the program and should be avoided if possible.

Similar threads

  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top